I have created a simple Django application which features a single HTML page for each US state. Each state page extend base.html but features different content.
URLs are stated in the URLs.py file and the rendering of the pages is mapped in views.py.
I’d like to know how I could scalably grow thw number of pages to hundreds or perhaps thousands, all with different names, without having to explicitly state each in the views.py and urls.py files.
What’s the correct way to do this?
HTML files
base.html
states_info_app/index.html
alabama-weather.html
alaska-population.html
arizona-schools.html
arkansas-hospitals.html
california-restaurants.html
colorado-colleges.html
connecticut-gyms.html
Views.py
from django.shortcuts import render
from django.views import View
def index(request):
return render(request, 'states_info_app/index.html')
def alabama(request):
return render(request, 'alabama-weather.html')
def alaska(request):
return render(request, 'alaska-population.html')
def arizona(request):
return render(request, 'arizona-schools.html')
def arkansas(request):
return render(request, 'arkansas-hospitals.html')
def california(request):
return render(request, 'california-restaurants.html')
def colorado(request):
return render(request, 'colorado-colleges.html')
def connecticut(request):
return render(request, 'connecticut-gyms.html')
URLs.py
from django.contrib import admin
from django.urls import path
from states_info_app.views import index
from states_info_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', index, name='index'),
path('states', views.states, name='states'),
path('alabama-weather', views.alabama, name='alabama'),
path('alaska-population', views.alaska, name='alaska'),
path('arizona-schools', views.arizona, name='arizona'),
path('arkansas-hospitals', views.arkansas, name='arkansas'),
path('california-restaurants', views.california, name='california'),
path('colorado-colleges', views.colorado, name='colorado'),
path('connecticut-gyms', views.connecticut, name='connecticut')
]
2
Answers
To efficiently add hundreds or thousands of HTML pages with different names in Django without explicitly stating each in the views.py and urls.py files, you can use dynamic URL routing and generic views. Here’s the correct way to achieve this:
Modify the URLs.py file:
Modify the Views.py file:
In this setup, we use a dynamic URL pattern slug:state_slug/ in urls.py, which will capture any state name and pass it to the StateDetailView. The StateDetailView is a generic class-based view that handles rendering the dynamic templates based on the state_slug captured from the URL.
With this implementation, you can easily add new HTML pages for each state without modifying the views.py or urls.py files. Just make sure to create corresponding HTML files for each state, following the naming convention, and Django will take care of the rest. For instance, if you add a new state "delaware", you simply create a new HTML file named "delaware.html" in the "states_info_app" templates folder, and it will be accessible via the URL "yourdomain.com/delaware/". This approach allows you to scale your application to handle a large number of state pages without any manual adjustments to the views and URLs configuration.
you could use a slug:
and as view: