Easier and Faster than flatpages. Rendering templates by simpling calling by his path on the URL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
views.py
"""

from django.views.generic.base import View
from django.template.response import TemplateResponse
from django.template.base import TemplateDoesNotExist
from django.http import Http404

class PageView(View):
    def get(self, request, *args, **kwargs):
        slug = kwargs['slug']
        # Fallback to search a index page if url ends with '/'
        slug = '%sindex' % slug if slug.endswith('/') else slug
        template = 'pages/%s.html' % slug
        response = TemplateResponse(request, template, {})

        # test if the template exists before the common middleware
        # try to automatically render the response
        try:
            response.resolve_template(template)
        except TemplateDoesNotExist:
            raise Http404('Page "%s" is not found' % slug)

        return response

"""
urls.py
"""

from django.conf.urls import patterns, url
from project.pages import views

urlpatterns = patterns('',
    url(r'^(?P<slug>.+)$', views.PageView.as_view()),
    url(r'^$', views.PageView.as_view(), {'slug':'welcome'}) # Easy to add aliases
)

More like this

  1. Class-based view mixin for flatpages by schwuk 1 year, 1 month ago
  2. Allow template tags in a Flatpage's content by kylefox 5 years, 9 months ago
  3. URL models by diverman 4 years, 6 months ago
  4. SSL Redirect Middleware by zbyte64 5 years, 9 months ago
  5. Flatpage Suggester Template tag for 404 templates by bradmontgomery 4 years, 2 months ago

Comments

umbrae (on April 2, 2012):

Does this not have a path vulnerability in it? Slug could be ../../../../../etc/passwd for example.

#

(Forgotten your password?)