Manoj Kamble
Manoj Kamble

Reputation: 595

How to render specific template if two app have same template name?

How can I render a specific template in Django? I have created three apps for my project. Each app contains a templates folder. The project structure is as follows:

├───Project
├───app1
│   ├───templates
├───app2
│   ├───templates
├───app3
│   ├───templates

In my app2 and app3, I have templates with the same name. I want to render the template from app3 but the template is rendering from app2. I am using the following code.

In app3.views.py

return render(request, "template_name.html")

By using the above code, the template gets rendered from app2. I want template should be rendered from app3.

Upvotes: 1

Views: 728

Answers (1)

Razenstein
Razenstein

Reputation: 3717

The general recommendation is to use a folder structure like app1/templates/app1/ in order to avoid such kind of collisions. Same for static files.

See also docs.djangoproject.com/en/4.0/intro/tutorial03 and search for "template namespacing"

Upvotes: 2

Related Questions