开发者

Difficulty overriding Django Admin template

开发者 https://www.devze.com 2023-02-08 09:28 出处:网络
I\'m using Django 1.2.4 on Ubuntu 10.10. I\'m trying to override the index.html开发者_Python百科 template for the admin module. I\'ve been following these instructions. I also looked at this question,

I'm using Django 1.2.4 on Ubuntu 10.10. I'm trying to override the index.html开发者_Python百科 template for the admin module. I've been following these instructions. I also looked at this question, but I'm still having difficulty.

The instructions say to create an admin directory in the templates directory:

templates/
    admin/
         index.html

I want to override a single block in the index.html. (Really, all I want to do is append some text to the end. Is there an easier way than copy/pasting the entire block and changing it?) (Update: Looks like {{block.super}} may help.)

To signal that I'm overriding, I put this at the top of my index.html:

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

Of course, that results in a stack overflow (from the terminal):

Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored

What is the correct way to do this? I tried a symlink per an answer on the linked question, but that resulted in the following:

me@mycomp:~/foo$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/ django_admin
[sudo] password for me: 
ln: creating symbolic link `django_admin': Protocol error

What am I doing wrong?


The recursion error is because you're extending the admin/index.html with itself.

You can either:

  • copy the entire admin/index.html template in your templates/admin/ directory, and it will replace the default template with yours
  • override the index.html per app or model, as explained here

I know this is late after the question, but you know, google traveling...


Amend settings.py with an extra template folder, for example:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "/home/mysite/webapps/django/myproject/templates",
    "/home/mysite/webapps/django/lib/python2.7/django/",  # extra folder
)

Then in myproject/templates/admin add your own index.html like:

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

{% block branding %}
    <h1 id="site-name">Administration for TheLittleButtonCo</h1>
{% endblock %}

Variations are possible, obviously. This works on Django 1.3.1 final


Not sure if you found the answer, but you need to change

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

to

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

as that is what the original index.html page overwrites. Because the Django system searches your templates folder before using the default admin one, so in this case it finds the admin/index.html in your templates, then it's trying to extend itself with the extend (hence the recursion error).

For reference you can customise the base_site.html in you templates too, it extends base.html. The best thing to do is copy the original from:

/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/

and paste it into your templates folder as a starting point


I use an extra package, called django-smart-extends

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号