You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
2.7 KiB

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