Django

Code

Changeset 8182

Show
Ignore:
Timestamp:
08/01/08 17:46:09 (5 months ago)
Author:
brosner
Message:

Initial admin template docs. Needs more work.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/admin.txt

    r8176 r8182  
    775775        ('^advanced-admin/(.*)', advanced_site.root), 
    776776    ) 
     777 
     778Overriding Admin Templates 
     779========================== 
     780 
     781It is relatively easy to override many of the templates which the admin module 
     782uses to generate the various pages of an admin site. You can even override a 
     783few of these templates for a specific app, or a specific model. 
     784 
     785Set up your project-level admin template directory 
     786-------------------------------------------------- 
     787 
     788The admin templates are located in the ``contrib/admin/templates/admin`` 
     789directory. 
     790 
     791In order to override one or more of them, first create an ``admin`` directory 
     792in your project-level ``templates`` directory. This can be any of the 
     793directories you specified in ``TEMPLATE_DIRS``. 
     794 
     795Within this ``admin`` directory, create sub-directories named after your app. 
     796Within these app subdirectories create sub-directories named after your models. 
     797Note, that the admin app will lowercase the model name when looking for the 
     798directory, so make sure you name the directory in all lowercase if you are 
     799going to run your app on a case-sensitive filesystem. 
     800 
     801To override an admin template for a specific app, copy and edit the template 
     802from the ``django/contrib/admin/templates/admin`` directory, and save it 
     803to one of the directories you just created. 
     804 
     805For example, if we wanted to add a tool to the change list view for all the 
     806models in an app named ``my_app``, we would copy 
     807``contrib/admin/templates/admin/change_list.html`` to the 
     808``templates/admin/my_app/`` directory of our project, and make any necessary 
     809changes. 
     810 
     811If we wanted to add a tool to the change list view for only a specific model 
     812named 'Page', we could copy that same file to the 
     813``templates/admin/my_app/page`` directory of our project. 
     814 
     815Overriding vs. replacing an admin template 
     816------------------------------------------ 
     817 
     818Because of the modular design of the admin templates, it is usually neither 
     819necessary nor advisable to replace an entire template. It is almost always 
     820better to override only the section of the template which you need to change. 
     821 
     822To continue the example above, we want to add a new link next to the ``History`` 
     823tool for the ``Page`` model. After looking at ``change_form.html`` we determine 
     824that we only need to override the ``object-tools`` block. Therefore here is our 
     825new ``change_form.html`` :: 
     826 
     827    {% extends "admin/change_form.html" %} 
     828    {% load i18n %} 
     829    {% block object-tools %} 
     830    {% if change %}{% if not is_popup %} 
     831      <ul class="object-tools"><li><a href="history/" class="historylink">{% trans "History" %}</a></li> 
     832      <li><a href="mylink/" class="historylink">My Link</a></li> 
     833      {% if has_absolute_url %}<li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif%} 
     834      </ul> 
     835    {% endif %}{% endif %} 
     836    {% endblock %} 
     837 
     838And that's it! If we placed this file in the ``templates/admin/my_app`` 
     839directory, our link would appear on every model's change form. 
     840 
     841Templates which may be overridden per app or model 
     842-------------------------------------------------- 
     843 
     844Not every template in ``contrib\admin\templates\admin`` may be overridden per 
     845app or per model. The following can: 
     846 
     847* ``change_form.html`` 
     848* ``change_list.html`` 
     849* ``delete_confirmation.html`` 
     850* ``object_history.html`` 
     851 
     852For those templates that cannot be overridden in this way, you may still 
     853override them for your entire project. Just place the new version in your 
     854``templates/admin`` directory. This is particularly useful to create custom 
     855404 and 500 pages. 
     856 
     857**Note:** Some of the admin templates, such as ``change_list_request.html`` are 
     858used to render custom inclusion tags. These may be overridden, but in such cases 
     859you are probably better off creating your own version of the tag in question and 
     860giving it a different name. That way you can use it selectively. 
     861 
     862Root and login templates 
     863------------------------ 
     864 
     865If you wish to change the index or login templates, you are better off creating 
     866your own ``AdminSite`` instance, and changing the ``index_template`` or 
     867``login_template`` properties.