How to write a single view function to render multiple html from diferent urls path?

陌路散爱 提交于 2019-12-02 10:50:17

问题


I am trying to avoid self coping of views functions but have no idea ho to do it. My views have a minor differences and there is definitely a way to render html pages with a single function in this case. I experimented with urls "name" value but failed. I just started with django and hope that experienced programers have a solution. Thank you for your help.

urls.py

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', views.index, name='index'),
        path('about/', views.about, name='about'),
        path('team/', views.team, name='team'),
        path('contacts/', views.contacts, name='contacts'),
        path('researches/', views.researches, name='researches'),
        path('publications/', views.publications, name='publications'),
    ]

and

views.py

def index(request):
    return render(request, 'website/index.html')

def about(request):
    return render(request, 'website/about.html')

def team(request):
    return render(request, 'website/team.html')

def publications(request):
    return render(request, 'website/publications.html')

def researches(request):
    return render(request, 'website/researches.html')

def contacts(request):
    return render(request, 'website/contacts.html')

回答1:


You can capture a slug in the URL and use it to determine which template to render.

path('<slug:slug>', views.general_page, ...)

...

def general_page(request, slug):
    return render(request, 'website/{}.html'.format(slug))



回答2:


If your site is completely static, you may consider using a static framework, like Jekyll.

This way you don't have to depend on the host server's features, and avoid issues that may arise when you use a complex framework like django.




回答3:


I would like to thank all people who tried to help me. Your advises inspired me for a solution. I used a code below and it works good. It appears that there is no need to create a separate view and you may use TemplateView for static html file rendering. That's exactly what I was looking for.

from django.contrib import admin
from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', TemplateView.as_view(template_name='index.html'), name="index"),
    path('about/', TemplateView.as_view(template_name='about.html'), name="about"),
    path('team/', TemplateView.as_view(template_name='team.html'), name="team"),
    path('contacts/', TemplateView.as_view(template_name='contacts.html'), name="contacts"),
    path('researches/', TemplateView.as_view(template_name='researches.html'), name="researches"),
    path('publications/', TemplateView.as_view(template_name='publications.html'), name="publications"),
]


来源:https://stackoverflow.com/questions/52003453/how-to-write-a-single-view-function-to-render-multiple-html-from-diferent-urls-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!