Problems extend change_form.html in django admin

gustavo.sdo picture gustavo.sdo · Feb 28, 2015 · Viewed 10.2k times · Source

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.

Answer

catavaran picture catavaran · Feb 28, 2015

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}