Problems extend change_form.html in django admin

12,968

Solution 1

change_form.html contains the following url tag:

{% url 'admin:app_list' app_label=opts.app_label %}

So you should pass the opts variable to the template context:

data = {'test': 'test',
        'opts': MyModel._meta}

UPDATE: The change_form.html template uses the {% submit_row %} template tag which requires some other context variables so the data dictionary should be like this:

data = {'test': 'test',
        'opts': MyModel._meta,    
        'change': True,
        'is_popup': False,
        'save_as': False,
        'has_delete_permission': False,
        'has_add_permission': False,
        'has_change_permission': False}

Solution 2

This is caused most likely because you have a {% url %} tag that is trying to link to the app_list. It could be in your admin/form_change.html or in some other included/extended template.

This is usually caused by context that is not passed correctly such as if you have a tag that looks like {% url 'app_list' %} or {% url 'app_list' var %} and the var is empty.

Share:
12,968

Related videos on Youtube

gustavo.sdo
Author by

gustavo.sdo

Updated on June 18, 2022

Comments

  • gustavo.sdo
    gustavo.sdo almost 2 years

    I'm trying to extend the change_form.html template of one of my models to include some information on the page. I've read the django documentation in https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#overriding-vs-replacing-an-admin-template

    The problem is that is occurring:

    NoReverseMatch at /contas_pagar/pagamento/2/ Reverse for 'app_list' with arguments '()' and keyword arguments '{u'app_label': ''}' not found. 1 pattern(s) tried: ['(?P\w+)/$']

    I'm using Django 1.6.5 with Django-Suit 0.2.12

    The error image: https://dl.dropboxusercontent.com/u/80415688/error_app_django.PNG

    in my_project/my_app/templates/admin/my_app/my_model/change_form.html

    {% extends "admin/change_form.html" %}
    

    in my_project/urls.py

    urlpatterns = patterns('',
        url(r'^contas_pagar/pagamento/(?P<id_parcela>\d+)/$',
                                 'contas_pagar.views.retorna_pagamentos_parcela'),
        # django urls
        url(r'^doc/', include('django.contrib.admindocs.urls')),
        url(r'', include(admin.site.urls)),)
    

    in my_project/views.py

    def return_id(request, id):
        data = { 'test': 'test', }
        return render_to_response('admin/my_app/my_model/change_form.html', data,
                                  context_instance=RequestContext(request))
    

    Does anyone have any idea how to solve?


    UPDATE:
    I made some changes to the code.
    The view is in my class ModelAdmin.

    in my_project/my_app/templates/admin/my_app/my_model/change_form.html:

    {% extends "admin/change_form.html" %}
    {% block after_field_sets %}{{ block.super }}
        <h2>{{ test }}</h2>
    {% endblock %}
    

    in my_project/my_app/admin.py:

    class PagamentoAdmin(admin.ModelAdmin):
    form = PagamentoForm
    model = Pagamento
    list_display = ('id', 'parcelas_contas_pagar', 'data', 'valor')
    
    def get_urls(self):
        urls = super(PagamentoAdmin, self).get_urls()
        my_urls = patterns('',
            (r'(?P<id_parcela>\d+)/$', self.admin_site.admin_view(self.retorna_pagamentos_parcela)),
        )
        return my_urls + urls
    
    def retorna_pagamentos_parcela(self, request, id_parcela):
        data = {
            'test': test,
            'opts': self.model._meta,
            'app_label': self.model._meta.app_label, 
            'change': True,
            'add': False,
            'is_popup': False,
            'save_as': False,
            'has_delete_permission': False,
            'has_add_permission': False,
            'has_change_permission': True
        }
        return render_to_response('admin/contas_pagar/pagamento/change_form.html', data, context_instance=RequestContext(request))
    

    Not appear more errors. Just is not displaying the fields of my class Admin.

  • gustavo.sdo
    gustavo.sdo about 9 years
    Hello friend, thanks for your response. I made the change in my view.py. He spent esso error ... But then there was another error ... see link I could not find anything about it. Any idea of this error?
  • gustavo.sdo
    gustavo.sdo about 9 years
    Thanks for the answer, my friend. Lacked move to the context opts variable. But still is showing the error said in answer I gave to @catavaran. Do you have any idea what can be?
  • gustavo.sdo
    gustavo.sdo about 9 years
    Amazing @catavaran, That was the problem, it was necessary to pass some variables there is more to the context :) But if you can help me with one last question ... I tried to do after your reply and I did not succeed ... The fields of my form was not rendered along with my customization. They simply do not appear. I need to provide some more variable in the context? extend some method of ModelAdmin?
  • catavaran
    catavaran about 9 years
    Show your view and template.
  • gustavo.sdo
    gustavo.sdo about 9 years
    Hi @catavaran, sorry for the delay. I updated my code in the post.
  • catavaran
    catavaran about 9 years
    If you are trying to show the full change form then you have to pass much more (~10) context vars. But I think it is a wrong way to do it. The proper way is to override the change_view(): docs.djangoproject.com/en/1.7/ref/contrib/admin/…
  • gustavo.sdo
    gustavo.sdo about 9 years
    I understand @catavaran, okay, I'll search on change_view. Thanks for your help :)