the_drow
the_drow

Reputation: 19181

Infinite recursion while extending the admin's app change_form template

I have the following template in template/admin/change_form.html:

{% extends "admin/change_form.html" %}
{% block extrahead %}
  {% include "dojango/base.html" %}
  {% block dojango_content %}
  {% endblock %}
{% endblock %}

However for some reason it throws a

TemplatesyntaxError: TemplateSyntaxError at /admin/cms/post/add/
Caught RuntimeError while rendering: maximum recursion depth exceeded while calling a Python object

Upvotes: 13

Views: 3241

Answers (6)

jcdude
jcdude

Reputation: 3011

The best way to do this that I have found is to use '..' to go up a couple of directories, then go back down into directories that should only be found in the Django code base.

As the Django templates are in something like "django/contrib/admin/templates/admin", I found that this worked me:

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

If that still causes a clash with some other structure you have, you could go further:

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

or even:

{% extends "../../../../django/contrib/admin/templates/admin/change_form.html" %}

Although it is a little hacky, at least by doing the above you don't have to use some other technique that involves copying the django source or setting up a symlink.

Upvotes: 1

Frank
Frank

Reputation: 909

I know it's late, but...

If extending - which is a far better option than duplicating - the key is to have it named anything except /admin/change_form.html.

(Although the OP referred to template/admin/change_form.html, this is simply because a path in his TEMPLATE_DIRS tuple ends in '/template' - mine generally end in '/templates' - but, these directories can be named anything and located anywhere.)

It will be used automatically on a per-app basis if named /admin/<MyAppName>/change_form.html

It will be used automatically on a per-model basis if named /admin/<MyAppName>/<MyModelName>/change_form.html

It can be named anything if specified explicitly in the ModelAdmin

class MyModelAdmin(admin.ModelAdmin):
    change_form_template = 'subdir/my_change_form.html'

Finally, if insistent on naming it /admin/change_form.html, you can - provided that the extends tag contains the full path to your django installation instead of a relative one.

Upvotes: 14

Goin
Goin

Reputation: 3964

With Django core it's impossible. But it is not impossible.

Copy and paste "the original template and change things you don't like" it's very ugly.

Don't make in the templates, whatever you don't make in python

This solution is to any template:

http://pypi.python.org/pypi/django-smart-extends

Upvotes: 0

Peter
Peter

Reputation: 21

I had the same problem. Solved by placing the overridden template under myapp/templates/admin/myapp instead of myapp/templates/admin.

Upvotes: 2

ricobl
ricobl

Reputation: 31

Also, you can point your AdminOptions class to another template using the change_form_template property.

Something like:

class MyOptions(AdminOptions):
    change_form_template = 'myapp/my_change_form.html'

And myapp/my_change_form.html:

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

Upvotes: 3

Ski
Ski

Reputation: 14487

You are in admin/change_form.html and you extend admin/change_form.html. You cannot extend the same template which you are in.

You probably expected that if you override template from admin application, you can extend the one you override. But this is not how it works. When you override a template you cannot access it.

Solution to your problem is to copy original template and change things you don't like.

Upvotes: 4

Related Questions