Explain template location in Django

Asked

Viewed 183 times

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?

1 answer

1

According to Django’s documentation:

get_template(template_name , using = None)

This function loads the template with the given name and returns a Template object. The exact type of return value depends on the backend that loaded the template. Each backend has its own Template class . get_template() attempts each model mechanism in order until one is successful. If the model cannot be found, it generates TemplateDoesNotExist . If the template is found but contains an invalid syntax, it will increase the TemplateSyntaxError. The way models are searched and loaded depends on the backend and configuration of each mechanism. If you want to restrict the search to a specific model engine, pass the NAME of the mechanism in the argument using.

  • But how do I set up the backend?

  • Could show your view ?

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

  • I believe it’s enough for you to TEMPLATES change positions in your DIRS thus: 'DIRS': [
 os.path.join(BASE_DIR, 'core/templates'), 
 os.path.join(BASE_DIR, 'tests/templates'), ] If it works out, let me know.

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

  • You wanted to be able to specify which application the template belongs to ?

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

Show 3 more comments

Browser other questions tagged

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