How To Write A Single View Function To Render Multiple Html From Diferent Urls Path?
Solution 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))
Solution 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.
Solution 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"),
]
Solution 4:
This work for me i import the views.py in url.py.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from . import views
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'),
]
or
from layout.views import index,team,about ...
just import your views which folder is located, in this case i import views from the folder(app) layout
Post a Comment for "How To Write A Single View Function To Render Multiple Html From Diferent Urls Path?"