Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems extend change_form.html in django admin

Tags:

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.

like image 886
gustavo.sdo Avatar asked Feb 28 '15 02:02

gustavo.sdo


People also ask

Can you extend more than one template Django?

Use Django extends in multiple templates Note that you can extend the header. html to multiple HTML documents using the same document structure and DTL block content tags. This means the header can render in multiple templates. The example above extends the header template to a new HTML template called contact.

Can I use Django admin as frontend?

Django Admin is one of the most important tools of Django. It's a full-fledged application with all the utilities a developer need. Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.

Where is admin HTML Django?

To view the default admin template you can access it in the django/contrib/admin/templates/admin folder. In this situation you will most likely be using a virtual environment and can find this folder in the directory that contains all the installed libraries.


1 Answers

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} 
like image 83
catavaran Avatar answered Sep 20 '22 00:09

catavaran