michael-mammut
michael-mammut

Reputation: 2783

Same filename in differen Django apps opens the same template

Currently I create a Django Website. I created one app.

project
--app
----templates
------index.html
----url.py
----views.py
--project
----templates
------index.html
----url.py
----views.py

in both url.py i create the url-pattern url(r'^', views.index, name="index"), and the url.py file in project contains url(r'^heatingControll/', include('heatingControll.urls')),.

In both views I add the function :

def index(request):
    template = loader.get_template('index.html')
    context = {}
    return HttpResponse(template.render(context, request))

As I understand, Django will opens the index.html from the app/template folder by run 127.0.0.1:8000/app, and by run 127.0.0.1:8000 the index.html from the project/templatefolder.

But it runs each time the app/templates/index.html file.

I strongly belive that it should be possible to use the same directory name in serveral apps.

What could be my problem?

Upvotes: 0

Views: 751

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599866

No, that's not how it works. You need to namespace the templates, even if they're inside an app.

--app
----templates
------app
--------index.html

or, just keep the single project-level templates file, but still use namespaces:

--project
----templates
------index.html
------app
--------index.html

Either way, you now reference the template as "app/index.html".

Note, you should really use the render shortcut:

def index(request):
    context = {}
    return render(request, 'app/index.html', context)

Also note, it's not usual to have a project-level views.py. Normally that would be inside an app.

Upvotes: 4

Related Questions