|
0
|
1 |
from django.contrib.flatpages.models import FlatPage |
|
|
2 |
from django.template import loader, RequestContext |
|
|
3 |
from django.shortcuts import get_object_or_404 |
|
|
4 |
from django.http import HttpResponse, HttpResponseRedirect |
|
|
5 |
from django.conf import settings |
|
|
6 |
from django.core.xheaders import populate_xheaders |
|
|
7 |
from django.utils.safestring import mark_safe |
|
29
|
8 |
from django.views.decorators.csrf import csrf_protect |
|
0
|
9 |
|
|
|
10 |
DEFAULT_TEMPLATE = 'flatpages/default.html' |
|
|
11 |
|
|
29
|
12 |
# This view is called from FlatpageFallbackMiddleware.process_response |
|
|
13 |
# when a 404 is raised, which often means CsrfViewMiddleware.process_view |
|
|
14 |
# has not been called even if CsrfViewMiddleware is installed. So we need |
|
|
15 |
# to use @csrf_protect, in case the template needs {% csrf_token %}. |
|
|
16 |
@csrf_protect |
|
0
|
17 |
def flatpage(request, url): |
|
|
18 |
""" |
|
|
19 |
Flat page view. |
|
|
20 |
|
|
|
21 |
Models: `flatpages.flatpages` |
|
|
22 |
Templates: Uses the template defined by the ``template_name`` field, |
|
|
23 |
or `flatpages/default.html` if template_name is not defined. |
|
|
24 |
Context: |
|
|
25 |
flatpage |
|
|
26 |
`flatpages.flatpages` object |
|
|
27 |
""" |
|
|
28 |
if not url.endswith('/') and settings.APPEND_SLASH: |
|
|
29 |
return HttpResponseRedirect("%s/" % request.path) |
|
|
30 |
if not url.startswith('/'): |
|
|
31 |
url = "/" + url |
|
|
32 |
f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID) |
|
|
33 |
# If registration is required for accessing this page, and the user isn't |
|
|
34 |
# logged in, redirect to the login page. |
|
|
35 |
if f.registration_required and not request.user.is_authenticated(): |
|
|
36 |
from django.contrib.auth.views import redirect_to_login |
|
|
37 |
return redirect_to_login(request.path) |
|
|
38 |
if f.template_name: |
|
|
39 |
t = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) |
|
|
40 |
else: |
|
|
41 |
t = loader.get_template(DEFAULT_TEMPLATE) |
|
|
42 |
|
|
|
43 |
# To avoid having to always use the "|safe" filter in flatpage templates, |
|
|
44 |
# mark the title and content as already safe (since they are raw HTML |
|
|
45 |
# content in the first place). |
|
|
46 |
f.title = mark_safe(f.title) |
|
|
47 |
f.content = mark_safe(f.content) |
|
|
48 |
|
|
|
49 |
c = RequestContext(request, { |
|
|
50 |
'flatpage': f, |
|
|
51 |
}) |
|
|
52 |
response = HttpResponse(t.render(c)) |
|
|
53 |
populate_xheaders(request, response, FlatPage, f.id) |
|
|
54 |
return response |