Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override templates of external app in Django

I want to override the templates of an external app (allauth, installed in site packages). Unfortunately no advice i read worked. I added the following to my settings.py:

PROJECT_ROOT = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates', 'allauth'))

and copied all templates (this content) to my_project_dir/templates/allauth. But when I restart the server and reload the page I only get the rendered templates from the original allauth app in site packages, not mine custom templates. Any hints?

like image 664
Zardoz Avatar asked Jul 29 '13 08:07

Zardoz


People also ask

How do I override a Django template?

You can either put template overrides in your project's templates directory or in an application's templates directory. If you have app and project templates directories that both contain overrides, the default Django template loader will try to load the template from the project-level directory first.

What does {% include %} do in Django?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.

What does {% mean in Django?

{% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.


2 Answers

Check the INSTALLED_APPS order, the first template matched will be rendered.

With this in mind you can just add the template under the same path in a custom app.

like image 126
ulysses Avatar answered Oct 17 '22 22:10

ulysses


The way I tend to figure out what's going on (with DEBUG set to True), is to have a view render a template that didn't exist, and look at the list of locations Django tried to load templates from (which will be included in the error page output).

What templates are the views trying to render? If they're trying to render allauth/foo.html, then you'll want to add my_project_dir/templates to your TEMPLATE_DIRS setting, not my_project_dir/templates/allauth.

like image 23
Dominic Rodger Avatar answered Oct 18 '22 00:10

Dominic Rodger