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.
- So, I configured
myproject/settings.pyto let load the template fromtemplatesfolder:'DIRS': ['templates'],. - In the app folder
myproject/apps/faqI have created atemplatesfolder. - In the app views
myproject/apps/faq/views.pyI 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)
- 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
templatesfolder and use the projectviews.pyandurls.pyand it didn't work I decided to create another app calledmain.
- In the app folder
myproject/apps/mainI have created atemplatesfolder. - In the app views
myproject/apps/main/views.pyI defined just a simple view to render the app list:
from django.shortcuts import render
def index_view(request):
return render(request, 'index.html')
- In the app folder I have created also a
urls.pyand added the previous view:
from django.urls import path
from .views import index_view
urlpatterns = [
path('', index_view, name='site-index'),
]
- 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
- templates
- main
- templates
- base.html
- index.html
- urls.py
- views.py
- templates
- faq
- settings.py
- urls.py
- apps
I am doing something wrong. So, any clues?