Dimitris Kougioumtzis Blog About technology

Dynamic TemplateView

Category Django

Posted on Jan. 8, 2018



Dynamic TemplateView


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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'

About

My name is Dimitris Kougioumtzis and i work as a Web developer at Rapidbounce Company

Elsewhere