Explain template location in Django

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, the tests / templates and the core / templates, when I use the get_template method it takes from any of the directories.

If you create a file named "create.html" in both directories, it takes what is inside my "application" where the "view.py" file is located, if I remove the "create.html" file from "tests ", it automatically takes what is in the" core "folder.

  

Django separates the code into "applications", which functions as a sort   of module.

I would like to define which folder (application) my template comes from.

UPDATE

I found in the documentation a form, but I found it redundant, to put the same application name inside the folder template, if there already exists the separation of the application, why would create another separation within template? Is there any way to use the application name itself for this?

    
asked by anonymous 25.07.2018 / 02:13

1 answer

1

According to the Django 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 model. Each backend has its own class Template. get_template() tries each model engine in order until one is successful. If the model can not be found, it generates TemplateDoesNotExist . If the template is found, but contains an invalid syntax, it will increase TemplateSyntaxError . The way models are searched and loaded depends on the backend and configuration of each engine. If you want to restrict the search to a particular model engine, pass the NAME of the engine to the using argument.

    
25.07.2018 / 03:26