How to override and extend basic Django admin templates?

前端 未结 11 871
长发绾君心
长发绾君心 2020-11-22 14:11

How do I override an admin template (e.g. admin/index.html) while at the same time extending it (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-r

相关标签:
11条回答
  • 2020-11-22 14:41

    Chengs's answer is correct, howewer according to the admin docs not every admin template can be overwritten this way: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#overriding-admin-templates

    Templates which may be overridden per app or model

    Not every template in contrib/admin/templates/admin may be overridden per app or per model. The following can:

    app_index.html
    change_form.html
    change_list.html
    delete_confirmation.html
    object_history.html
    

    For those templates that cannot be overridden in this way, you may still override them for your entire project. Just place the new version in your templates/admin directory. This is particularly useful to create custom 404 and 500 pages

    I had to overwrite the login.html of the admin and therefore had to put the overwritten template in this folder structure:

    your_project
     |-- your_project/
     |-- myapp/
     |-- templates/
          |-- admin/
              |-- login.html  <- do not misspell this
    

    (without the myapp subfolder in the admin) I do not have enough repution for commenting on Cheng's post this is why I had to write this as new answer.

    0 讨论(0)
  • 2020-11-22 14:42

    You can use django-overextends, which provides circular template inheritance for Django.

    It comes from the Mezzanine CMS, from where Stephen extracted it into a standalone Django extension.

    More infos you find in "Overriding vs Extending Templates" (http:/mezzanine.jupo.org/docs/content-architecture.html#overriding-vs-extending-templates) inside the Mezzanine docs.

    For deeper insides look at Stephens Blog "Circular Template Inheritance for Django" (http:/blog.jupo.org/2012/05/17/circular-template-inheritance-for-django).

    And in Google Groups the discussion (https:/groups.google.com/forum/#!topic/mezzanine-users/sUydcf_IZkQ) which started the development of this feature.

    Note:

    I don't have the reputation to add more than 2 links. But I think the links provide interesting background information. So I just left out a slash after "http(s):". Maybe someone with better reputation can repair the links and remove this note.

    0 讨论(0)
  • 2020-11-22 14:44

    This site had a simple solution that worked with my Django 1.7 configuration.

    FIRST: Make a symlink named admin_src in your project's template/ directory to your installed Django templates. For me on Dreamhost using a virtualenv, my "source" Django admin templates were in:

    ~/virtualenvs/mydomain/lib/python2.7/site-packages/django/contrib/admin/templates/admin
    

    SECOND: Create an admin directory in templates/

    So my project's template/ directory now looked like this:

    /templates/
       admin
       admin_src -> [to django source]
       base.html
       index.html
       sitemap.xml
       etc...
    

    THIRD: In your new template/admin/ directory create a base.html file with this content:

    {% extends "admin_src/base.html" %}
    
    {% block extrahead %}
    <link rel='shortcut icon' href='{{ STATIC_URL }}img/favicon-admin.ico' />
    {% endblock %}
    

    FOURTH: Add your admin favicon-admin.ico into your static root img folder.

    Done. Easy.

    0 讨论(0)
  • 2020-11-22 14:46

    for app index add this line to somewhere common py file like url.py

    admin.site.index_template = 'admin/custom_index.html'
    

    for app module index : add this line to admin.py

    admin.AdminSite.app_index_template = "servers/servers-home.html"
    

    for change list : add this line to admin class:

    change_list_template = "servers/servers_changelist.html"
    

    for app module form template : add this line to your admin class

    change_form_template = "servers/server_changeform.html"
    

    etc. and find other in same admin's module classes

    0 讨论(0)
  • 2020-11-22 14:50

    I agree with Chris Pratt. But I think it's better to create the symlink to original Django folder where the admin templates place in:

    ln -s /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/ templates/django_admin
    

    and as you can see it depends on python version and the folder where the Django installed. So in future or on a production server you might need to change the path.

    0 讨论(0)
提交回复
热议问题