I created a simple Django application that creates a separate HTML page for each US state. Each state's page extends base.html, but with different content.
URLs are declared in the URLs.py file, and the rendering of the page is mapped in views.py.
I would like to know how I can scalably increase the number of pages, possibly into the hundreds or even thousands, without explicitly declaring the name of each page in the views.py and urls.py files.
How to implement this correctly?
HTML file
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') ]
P粉5961619152023-07-21 14:28:09
To efficiently add hundreds or thousands of HTML pages with different names in Django without explicitly declaring each page in views.py and urls.py files, you can use dynamic URL routing and Universal view. Here is the correct way to do it:
Modify the urls.py file:
from django.contrib import admin from django.urls import path from states_info_app.views import index, StateDetailView from states_info_app import views urlpatterns = [ path('admin/', admin.site.urls), path('', index, name='index'), path('states', views.states, name='states'), path('<slug:state_slug>/', StateDetailView.as_view(), name='state-detail') ]
Modify views.py file:
from django.shortcuts import render from django.views import View from django.views.generic import DetailView class StateDetailView(DetailView): template_name = 'states_info_app/base.html' model = YourStateModel # Replace this with the actual model you're using for states def get_template_names(self): state_slug = self.kwargs['state_slug'] return [f'states_info_app/{state_slug}.html']
In this setup we are using a dynamic URL pattern slug:state_slug/ in urls.py which will capture the name of any state and pass it to the StateDetailView. StateDetailView is a generic class-based view that renders 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 the corresponding HTML files for each state, follow the naming convention, and Django will take care of the rest. For example, if you added a new state named "delaware", simply create a new HTML file named "delaware.html" in the "states_info_app" template folder, which will be accessed via the URL "yourdomain.com/delaware" /"access. This approach enables you to scale your application to handle large numbers of state pages without having to manually adjust views and URL configurations.