0
I’m using Django for a project, but I came across a question.
In my file settings.py
I have the following configuration of templates:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'tests/templates'),
os.path.join(BASE_DIR, 'core/templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
And the following method in my file view.py
Django uses the MTV model, so the "view" is equivalent to the controller in the MVC model
def index(request):
template = loader.get_template('create.html')
return HttpResponse(template.render())
I have two directories "tests/templates" and "core/templates" when using the method get_template
it picks up from any of the directories.
If I create a file called "create.html" in both directories it takes what is inside my "application" where the file "view.py" is found, if I remove the file "create.html" from "tests", automatically it takes what is in the folder "core"".
Django separates the code into "applications", which acts as a species module.
I would like to define which folder (application) my template comes from.
UPDATE
I found in the documentation a way, but found it redundant, to put the same application name inside the template folder, if there is already the separation of the application, why create another separation inside the template? Is there any way to use the application name for this?
But how do I set up the backend?
– Wictor Chaves
Could show your
view
?– ThiagoO
In my view this way Return render(request, 'tests/create.html', {'form': form}), but I had to create inside the template folder a folder called tests.
– Wictor Chaves
I believe it’s enough for you to
TEMPLATES
change positions in yourDIRS
thus:'DIRS': [
 os.path.join(BASE_DIR, 'core/templates'), 
 os.path.join(BASE_DIR, 'tests/templates'), ]
If it works out, let me know.– ThiagoO
So, this working, the problem is not to leave working, this I have already managed, as I put the question I saw how it works in the documentation, the problem is that I wanted to automatically get the name of the application not to need to create folders inside template.
– Wictor Chaves
You wanted to be able to specify which application the template belongs to ?
– ThiagoO
This, I can do this by creating a folder inside the template folder, but I find it unnecessary, since I already have the name of the application to differentiate it.
– Wictor Chaves
Let’s go continue this discussion in chat.
– ThiagoO