| 1 |
from django.conf import settings |
| 2 |
from django.contrib.flatpages.models import FlatPage |
| 3 |
from django.contrib.sites.shortcuts import get_current_site |
| 4 |
from django.http import Http404, HttpResponse, HttpResponsePermanentRedirect |
| 5 |
from django.shortcuts import get_object_or_404 |
| 6 |
from django.template import loader |
| 7 |
from django.utils.safestring import mark_safe |
| 8 |
from django.views.decorators.csrf import csrf_protect |
| 9 |
|
| 10 |
DEFAULT_TEMPLATE = "flatpages/default.html" |
| 11 |
|
| 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 |
# However, we can't just wrap this view; if no matching flatpage exists, |
| 17 |
# or a redirect is required for authentication, the 404 needs to be returned |
| 18 |
# without any CSRF checks. Therefore, we only |
| 19 |
# CSRF protect the internal implementation. |
| 20 |
|
| 21 |
|
| 22 |
def flatpage(request, url): |
| 23 |
""" |
| 24 |
Public interface to the flat page view. |
| 25 |
|
| 26 |
Models: `flatpages.flatpages` |
| 27 |
Templates: Uses the template defined by the ``template_name`` field, |
| 28 |
or :template:`flatpages/default.html` if template_name is not defined. |
| 29 |
Context: |
| 30 |
flatpage |
| 31 |
`flatpages.flatpages` object |
| 32 |
""" |
| 33 |
if not url.startswith("/"): |
| 34 |
url = "/" + url |
| 35 |
site_id = get_current_site(request).id |
| 36 |
try: |
| 37 |
f = get_object_or_404(FlatPage, url=url, sites=site_id) |
| 38 |
except Http404: |
| 39 |
if not url.endswith("/") and settings.APPEND_SLASH: |
| 40 |
url += "/" |
| 41 |
f = get_object_or_404(FlatPage, url=url, sites=site_id) |
| 42 |
return HttpResponsePermanentRedirect("%s/" % request.path) |
| 43 |
else: |
| 44 |
raise |
| 45 |
return render_flatpage(request, f) |
| 46 |
|
| 47 |
|
| 48 |
@csrf_protect |
| 49 |
def render_flatpage(request, f): |
| 50 |
""" |
| 51 |
Internal interface to the flat page view. |
| 52 |
""" |
| 53 |
# If registration is required for accessing this page, and the user isn't |
| 54 |
# logged in, redirect to the login page. |
| 55 |
if f.registration_required and not request.user.is_authenticated: |
| 56 |
from django.contrib.auth.views import redirect_to_login |
| 57 |
|
| 58 |
return redirect_to_login(request.path) |
| 59 |
if f.template_name: |
| 60 |
template = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) |
| 61 |
else: |
| 62 |
template = loader.get_template(DEFAULT_TEMPLATE) |
| 63 |
|
| 64 |
# To avoid having to always use the "|safe" filter in flatpage templates, |
| 65 |
# mark the title and content as already safe (since they are raw HTML |
| 66 |
# content in the first place). |
| 67 |
f.title = mark_safe(f.title) |
| 68 |
f.content = mark_safe(f.content) |
| 69 |
|
| 70 |
return HttpResponse(template.render({"flatpage": f}, request)) |