Django load wrong template

Viewed 344

I am new to Django. I am trying to configure an app called 'faq', and an index page to display the different "apps", including the previous one.

I was following the Learn Django site guide to decide what was best for the template structure.

  1. So, I configured myproject/settings.py to let load the template from templates folder: 'DIRS': ['templates'],.
  2. In the app folder myproject/apps/faq I have created a templates folder.
  3. In the app views myproject/apps/faq/views.py I defined the views to load the data from database and send it back to the template:
from django.shortcuts import render
from .models import *

def faqs_view(request):
    faqs = FrequentAskedQuestion.objects.all()
    context = { 'faqs': faqs }
    return render(request, 'index.html', context)

def faq_view(request, id):
    faq = FrequentAskedQuestion.objects.get(id=id)
    print(faq)
    context = { 'faq': faq }
    return render(request, 'detail.html', context)
  1. In the app folder I have created a URLconf configuration in myproject/apps/faq/urls.py:
from django.urls import path
from .views import faqs_view, faq_view

urlpatterns = [
    path('', faqs_view, name='faq-list'),
    path('<int:id>', faq_view, name='faq-detail'),
]

A parenthesis here, since the first attempt I have tried to create a project templates folder and use the project views.py and urls.pyand it didn't work I decided to create another app called main.

  1. In the app folder myproject/apps/main I have created a templates folder.
  2. In the app views myproject/apps/main/views.py I defined just a simple view to render the app list:
from django.shortcuts import render

def index_view(request):
    return render(request, 'index.html')
  1. In the app folder I have created also a urls.py and added the previous view:
from django.urls import path
from .views import index_view

urlpatterns = [
    path('', index_view, name='site-index'),
]
  1. In the project URLconf file I have added both apps:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('myproject.apps.main.urls'), name='site-index'),
    path('admin/', admin.site.urls, name='admin-index'),
    path('faq/', include('myproject.apps.faq.urls'), name='faq-index'),
]

After the previous setup I got working 'faq' app and it works great. But, whenever I go to http://localhost:8000/ it display the title from myproject/apps/faq/templates/index when it should display myproject/apps/main/templates/index.

Maybe it is worth to mention that both templates (main/templates/index and faq/templates/index) inherit from a base template that is created in each app template folder.

The file tree:

  • myproject
    • apps
      • faq
        • templates
          • base.html
          • index.html
        • urls.py
        • views.py
      • main
        • templates
          • base.html
          • index.html
        • urls.py
        • views.py
    • settings.py
    • urls.py

I am doing something wrong. So, any clues?

1 Answers

Typically you write the name of the app under the templates directory, so:

faq/
  templates/
    faq/
      index.html

This creates a sort of "namespace", such that you can reference to the correct file with:

from django.shortcuts import render

def index_view(request):
    return render(request, 'faq/index.html')

If you do not construct such directory, then Djangno will simply search for a file named index.html in all the template directories. So if there are multiple, it depends on the order in which the template directories are searched.

Related