| | 854 | Overriding Admin Templates |
|---|
| | 855 | ========================== |
|---|
| | 856 | |
|---|
| | 857 | It is relatively easy to override many of the templates which the admin module |
|---|
| | 858 | uses to generate the various pages of an admin site. You can even override a few |
|---|
| | 859 | of these templates for a specific app, or a specific model. |
|---|
| | 860 | |
|---|
| | 861 | Set up your projects admin template directories |
|---|
| | 862 | ----------------------------------------------- |
|---|
| | 863 | |
|---|
| | 864 | The admin template files are located in the ``contrib/admin/templates/admin`` |
|---|
| | 865 | directory. |
|---|
| | 866 | |
|---|
| | 867 | In order to override one or more of them, first create an ``admin`` directory in |
|---|
| | 868 | your project's ``templates`` directory. This can be any of the directories you |
|---|
| | 869 | specified in ``TEMPLATE_DIRS``. |
|---|
| | 870 | |
|---|
| | 871 | Within this ``admin`` directory, create sub-directories named after your app. |
|---|
| | 872 | Within these app subdirectories create sub-directories named after your models. |
|---|
| | 873 | Note, that the admin app will lowercase the model name when looking for the |
|---|
| | 874 | directory, so make sure you name the directory in all lowercase if you are going |
|---|
| | 875 | to run your app on a case-sensitive filesystem. |
|---|
| | 876 | |
|---|
| | 877 | To override an admin template for a specific app, copy and edit the template |
|---|
| | 878 | from the ``django/contrib/admin/templates/admin`` directory, and save it to one |
|---|
| | 879 | of the directories you just created. |
|---|
| | 880 | |
|---|
| | 881 | For example, if we wanted to add a tool to the change list view for all the |
|---|
| | 882 | models 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 |
|---|
| | 885 | changes. |
|---|
| | 886 | |
|---|
| | 887 | If we wanted to add a tool to the change list view for only a specific model |
|---|
| | 888 | named 'Page', we would copy that same file to the |
|---|
| | 889 | ``templates/admin/my_app/page`` directory of our project. |
|---|
| | 890 | |
|---|
| | 891 | Overriding vs. replacing an admin template |
|---|
| | 892 | ------------------------------------------ |
|---|
| | 893 | |
|---|
| | 894 | Because of the modular design of the admin templates, it is usually neither |
|---|
| | 895 | necessary nor advisable to replace an entire template. It is almost always |
|---|
| | 896 | better to override only the section of the template which you need to change. |
|---|
| | 897 | |
|---|
| | 898 | To continue the example above, we want to add a new link next to the ``History`` |
|---|
| | 899 | tool for the ``Page`` model. After looking at ``change_form.html`` we determine |
|---|
| | 900 | that we only need to override the ``object-tools`` block. Therefore here is our |
|---|
| | 901 | new ``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 | |
|---|
| | 919 | And that's it! If we placed this file in the ``templates/admin/my_app`` |
|---|
| | 920 | directory, our link would appear on every model's change form. |
|---|
| | 921 | |
|---|
| | 922 | Templates which may be overridden per app or model |
|---|
| | 923 | -------------------------------------------------- |
|---|
| | 924 | |
|---|
| | 925 | Not every template in ``contrib\admin\templates\admin`` may be overridden per |
|---|
| | 926 | app or per model. The following can: |
|---|
| | 927 | |
|---|
| | 928 | * ``change_form.html`` |
|---|
| | 929 | * ``change_list.html`` |
|---|
| | 930 | * ``delete_confirmation.html`` |
|---|
| | 931 | * ``object_history.html`` |
|---|
| | 932 | |
|---|
| | 933 | For those templates that cannot be overridden in this way, you may still |
|---|
| | 934 | override them for your entire project. Just place the new version in your |
|---|
| | 935 | ``templates/admin`` directory. This is particularly useful to create custom 404 |
|---|
| | 936 | and 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 | |
|---|
| | 945 | Root and login templates |
|---|
| | 946 | ------------------------ |
|---|
| | 947 | |
|---|
| | 948 | If you wish to change the index or login templates, you are better off creating |
|---|
| | 949 | your own ``AdminSite`` instance (see below), and changing the ``index_template`` |
|---|
| | 950 | or ``login_template`` properties. |
|---|
| | 951 | |
|---|