Dynamic TemplateView

Django -- Posted on Jan. 8, 2018

Dynamic TemplateView

              
                in urls.py

from .views import PageTemplateView  

and the urlpattern 

    url(r'^(?P<slug>[-\w]+)/$', PageTemplateView.as_view(),
        name='pages'),

in views.py 

from django.views.generic import TemplateView

class PageTemplateView(TemplateView):

    def get_template_names(self):
        templates = []
        template = 'pages/{}.html'.format(self.kwargs['slug'])
        templates.append(template)
        return templates


Then in  project root templates folder  create the folder with name pages. Add a static page for example contact.html  so the full path  is 'pages/contact.html'
                  
   
            

Related Posts