How to override and extend basic Django admin templates?

前端 未结 11 869
长发绾君心
长发绾君心 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.

提交回复
热议问题