How to get a template that’s in another app on Django

Asked

Viewed 452 times

0

Hey, guys, you okay? So people are starting now to program in Django I’m really enjoying, only a question arose, I wonder if you have how to leave the template

html base.

as "Universal" what I mean is that I do not want to keep creating several times the base.html in each app I create, to be clearer I will put a print here

inserir a descrição da imagem aqui

The green arrow is the main one, I mean, it’s where I want to inherit the base but as I wasn’t getting it I ended up doing the red arrow but this is not functional because if I want to add a link in the menu for example I will have to tinker with the base.html of each app and this ends up making me waste time.

Well, if it’s unclear, comment to me that I’ll try to explain it differently. Thanks in advance :).

PS: I tried to put in Static folder and it didn’t work.

2 answers

0


  • 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.

  • i wanted a way to extend a template from another app

  • 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.

  • 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

  • I’m glad it worked out, Maurice. Good studies!

  • 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.

Show 1 more comment

0

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 {% extendsmaster/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", ...) )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.