首頁  >  問答  >  主體

在Django中高效率地新增多個HTML頁面

我創建了一個簡單的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粉356128676P粉356128676457 天前560

全部回覆(1)我來回復

  • P粉596161915

    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配置。

    回覆
    0
  • 取消回覆