Django

Code

Changeset 8858

Show
Ignore:
Timestamp:
09/02/08 11:47:45 (4 months ago)
Author:
jacob
Message:

Fixed #7943: added documentation on overriding admin templates. Thanks, mwdiers

Files:

Legend:

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

    r8856 r8858  
    852852information. 
    853853 
     854Overriding Admin Templates 
     855========================== 
     856 
     857It is relatively easy to override many of the templates which the admin module 
     858uses to generate the various pages of an admin site. You can even override a few 
     859of these templates for a specific app, or a specific model. 
     860 
     861Set up your projects admin template directories 
     862----------------------------------------------- 
     863 
     864The admin template files are located in the ``contrib/admin/templates/admin`` 
     865directory. 
     866 
     867In order to override one or more of them, first create an ``admin`` directory in 
     868your project's ``templates`` directory. This can be any of the directories you 
     869specified in ``TEMPLATE_DIRS``. 
     870 
     871Within this ``admin`` directory, create sub-directories named after your app. 
     872Within these app subdirectories create sub-directories named after your models. 
     873Note, that the admin app will lowercase the model name when looking for the 
     874directory, so make sure you name the directory in all lowercase if you are going 
     875to run your app on a case-sensitive filesystem. 
     876 
     877To override an admin template for a specific app, copy and edit the template 
     878from the ``django/contrib/admin/templates/admin`` directory, and save it to one 
     879of the directories you just created. 
     880 
     881For example, if we wanted to add a tool to the change list view for all the 
     882models in an app named ``my_app``, we would copy 
     883``contrib/admin/templates/admin/change_list.html`` to the 
     884``templates/admin/my_app/`` directory of our project, and make any necessary 
     885changes. 
     886 
     887If we wanted to add a tool to the change list view for only a specific model 
     888named 'Page', we would copy that same file to the 
     889``templates/admin/my_app/page`` directory of our project. 
     890 
     891Overriding vs. replacing an admin template 
     892------------------------------------------ 
     893 
     894Because of the modular design of the admin templates, it is usually neither 
     895necessary nor advisable to replace an entire template. It is almost always 
     896better to override only the section of the template which you need to change. 
     897 
     898To continue the example above, we want to add a new link next to the ``History`` 
     899tool for the ``Page`` model. After looking at ``change_form.html`` we determine 
     900that we only need to override the ``object-tools`` block. Therefore here is our 
     901new ``change_form.html`` :: 
     902 
     903    {% extends "admin/change_form.html" %} 
     904    {% load i18n %} 
     905    {% block object-tools %} 
     906    {% if change %}{% if not is_popup %} 
     907      <ul class="object-tools"> 
     908        <li><a href="history/" class="historylink">{% trans "History" %}</a></li> 
     909        <li><a href="mylink/" class="historylink">My Link</a></li> 
     910        {% if has_absolute_url %} 
     911            <li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink"> 
     912                {% trans "View on site" %}</a> 
     913            </li> 
     914        {% endif%} 
     915      </ul> 
     916    {% endif %}{% endif %} 
     917    {% endblock %} 
     918 
     919And that's it! If we placed this file in the ``templates/admin/my_app`` 
     920directory, our link would appear on every model's change form. 
     921 
     922Templates which may be overridden per app or model 
     923-------------------------------------------------- 
     924 
     925Not every template in ``contrib\admin\templates\admin`` may be overridden per 
     926app or per model. The following can: 
     927 
     928    * ``change_form.html`` 
     929    * ``change_list.html`` 
     930    * ``delete_confirmation.html`` 
     931    * ``object_history.html`` 
     932 
     933For those templates that cannot be overridden in this way, you may still 
     934override them for your entire project. Just place the new version in your 
     935``templates/admin`` directory. This is particularly useful to create custom 404 
     936and 500 pages. 
     937 
     938.. note:: 
     939 
     940    Some of the admin templates, such as ``change_list_request.html`` are used 
     941    to render custom inclusion tags. These may be overridden, but in such cases 
     942    you are probably better off creating your own version of the tag in question 
     943    and giving it a different name. That way you can use it selectively. 
     944 
     945Root and login templates 
     946------------------------ 
     947 
     948If you wish to change the index or login templates, you are better off creating 
     949your own ``AdminSite`` instance (see below), and changing the ``index_template`` 
     950or ``login_template`` properties. 
     951 
    854952``AdminSite`` objects 
    855953=====================