Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, June 2, 2016

Django: Overriding AND extending an app template

Django: Overriding AND extending an app template


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 loader checks before the app's template dir. If you just want to override certain blocks of the template you also have to copy the whole template ad change that block, which is actually not very DRY.

Does anybody know a way to override the orginial template, while at the same moment extending it, so that you just have to override the specific block you want to change? (the thing is doing this without changing the template's name, because in some cases you might have to change the view to make it work with another template)

EDIT: As Adam Taylor pointed out in the comments from Django 1.9 on this is possible without any hacks.

Answer by Paulo Scardine for Django: Overriding AND extending an app template


[update]

I've misread the question, my original answer applies only to the admin app, which has a built-in template extension mechanism. For other app that lacks such mechanism, I would just fork the original templates instead of fiddling with custom template loaders like the chosen answer advises. If you are worried about forking, you can also implement an extension mechanism and contribute back to the original project if you think it is worth.


[original answer]

Straight from the fine manual: Because of the modular design of the admin templates, it is usually neither necessary nor advisable to replace an entire template. It is almost always better to override only the section of the template which you need to change.

To continue the example above, we want to add a new link next to the History tool for the Page model. After looking at change_form.html we determine that we only need to override the object-tools block. Therefore here is our new change_form.html :

{% extends "admin/change_form.html" %}  {% load i18n %}  {% block object-tools %}  {% if change %}{% if not is_popup %}      {% endif %}{% endif %}  {% endblock %}  

And that's it! If we placed this file in the templates/admin/my_app directory, our link would appear on

every model's change form.

Answer by akaihola for Django: Overriding AND extending an app template


In the Django wiki, Simon Willison presents a trick to achieve the "self-extending template" effect. It isn't directly applicable if you're using the app_directories template loader, though.

Symlinking apps' templates directories inside a new directory might do a trick.

Answer by leech for Django: Overriding AND extending an app template


I think the answer from this related question is pertinent; currently, the best solution seems to be to use a custom template loader from the django-apptemplates package on PyPI, so you can just use pip to install it (e.g. pip install django-apptemplates).

The template loader allows you to extend a template in a specific app; for example, to extend the index page of the admin inteface, you would add

'apptemplates.Loader',  

to your TEMPLATE_LOADERS list in settings.py, and use

{% extends "admin:admin/index.html" %}  

in your templates.

Answer by seddonym for Django: Overriding AND extending an app template


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!

Answer by papirrin for Django: Overriding AND extending an app template


As an update since this seems to be a popular question. I have used the overextends app without any problem. It provides a new keywords overextends that allows to extend templates with the same name.

And it is easy to install with pip:

 pip install -U django-overextends  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.