Just inherit from "base.html" in another app, doing nothing else.
All "template" folders of all Jango apps are consolidated into a single namespace. In other words - when it comes to importing, extending or rendering templates - whether it’s pulling them from within a template or code, Django exposes all the template files in all the installed apps.
So if you have a 'master' app where you will leave the 'base.html' template that you want to re-use, you can do:
/
/mestre/
/mestre/templates
/mestre/templates/base.html
/app1
/app1/templates
/app1/templates/pagina_do_app1.html
and inside the archive pagina_do_app1.html
can use quietly {% extends 'base.html' %}
that it will pull the template into the file with that name even though it is in the other app. (Just make sure that the other app is installed in the installed_apps
of settings
of your Django project)
The only recommendation if you’re going to use templates between different projects is to add one more folder, inside the templates folder, with the name of the app - and put the templates of each app inside. This will prevent conflicts. For example, let’s assume that you add an extension of Django that also have a template base.html
- here which base.html it will pull, whether yours, or that extension, is basically luck (depends on the order in "installed_apps", I think) )
So to avoid this, create an extra level of directories so that your template is now pulled like {% extends
master/base.html%}
:
/
/mestre/
/mestre/templates
/mestre/templates/mestre/
/mestre/templates/mestre/base.html
/app1
/app1/templates
/app1/templates/app1
/app1/templates/app1/pagina_do_app1.html
(and in your Python code, of course, use app1 templates such as render(request, "app1/pagina_do_app1.html", ...)
)
Thanks for answering Douglas, so dude I had already taken a look at it and it really works, however when I will give extends in the base.html of another app it does not work.
– Mauricio Dantas
i wanted a way to extend a template from another app
– Mauricio Dantas
In the template of the other apps try to do the following: {% extends "app_config/base.html" %}. In theory it was supposed to work because Django will get this . html directly in the app_config templates folder.
– cokakola
It worked dude, I hadn’t really thought that way... I got stuck in the way we got in a file from another folder (.././) going back to the directories, I finally got, thank you very much guy, I could put as an answer this
– Mauricio Dantas
I’m glad it worked out, Maurice. Good studies!
– cokakola
Mauricio: if the answer did not resolve your problem, nay mark as accepted (green check) - you can give an upvote if you want. But it seems to me that the question has not been answered properly, only that as there is an answer accepted, no one else will be interested in answering.
– jsbueno