Django: Overriding AND extending an app template

前端 未结 6 2025
北荒
北荒 2020-12-02 18:05

If you want to override a template coming with an app in django (in app/templates/app/) you create a template of the same name in another directory, which the template loade

6条回答
  •  温柔的废话
    2020-12-02 19:06

    One simple way to do it is this:

    Let's say you want to extend and override django/contrib/admin/templates/admin/change_form.html.

    First, copy the original change_form.html to your app's template directory and rename it as something like myapp/templates/admin/original_change_form.html. (You could also do this as a symlink.)

    Second, create your own change_form.html in myapp/templates/admin. At the top of it, put the following:

    {% extends "admin/original_change_form.html" %}
    

    Simple!

提交回复
热议问题