我创建了一个简单的Django应用程序,为每个美国州都创建了一个单独的HTML页面。每个州的页面都扩展了base.html,但内容不同。
URLs在URLs.py文件中声明,页面的渲染在views.py中进行映射。
我想知道如何能够可扩展地增加页面数量,可能达到数百甚至数千个,而不需要在views.py和urls.py文件中显式地声明每个页面的名称。
如何正确地实现这一点?
HTML 文件
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
为了在Django中高效地添加数百或数千个具有不同名称的HTML页面,而无需在views.py和urls.py文件中显式地声明每个页面,您可以使用动态URL路由和通用视图。以下是正确的实现方法:
修改urls.py文件:
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') ]
修改views.py文件:
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']
在这个设置中,我们在urls.py中使用了一个动态的URL模式slug:state_slug/,它将捕获任何州的名称并将其传递给StateDetailView。StateDetailView是一个通用的基于类的视图,它根据从URL中捕获的state_slug来渲染动态模板。
通过这种实现方式,您可以轻松地为每个州添加新的HTML页面,而无需修改views.py或urls.py文件。只需确保为每个州创建相应的HTML文件,遵循命名约定,Django将处理其余部分。例如,如果您添加了一个名为"delaware"的新州,只需在"states_info_app"模板文件夹中创建一个名为"delaware.html"的新HTML文件,它将通过URL"yourdomain.com/delaware/"访问。这种方法使您能够扩展应用程序以处理大量的州页面,而无需手动调整视图和URL配置。