Custom 404 and 500 pages

43 peržiūros
Praleisti ir pereiti prie pirmo neskaityto pranešimo

Peter Farrell

neskaityta,
2019-11-22 16:30:452019-11-22
kam: django-cms...@googlegroups.com
Hi All,

I have a customer that needed to be able to customize their 404 and 500 pages for their multi-language DjangoCMS site. Using the CMS seems like an obvious solution since we couldn't use a static page, however being able to actually set it up was a pain. I was able to come up with the following, however it seems like this would be a common customization.

urls.py:
handler404 = 'shared.views.page_not_found'

shared/views.py:
from cms.views import details
from django.http import HttpResponse

def page_not_found(request, exception):
    response = details(request, '404')
    # The cache framework incorrectly caches this page if you directly state the 404 status on the response then serving 200 sequent requests.
    # This should work but does not:
    #  response.status_code = 404
    #  return response.render()
   # The workaround is get the rendered content from the CMS and put it in a new HttpResponse
    return HttpResponse(content=response.rendered_content, content_type='text/html; charset=utf-8', status=404)

Two things:

1) Not sure why the Django cache framework and DjangoCMS caches the page when setting the 404 directly (as shown in the comments above). I cannot find in the response.render where the status_code gets clobbered back to 200.  However clearly if I grab the content (not the headers or statuscode) and return that in a new HttpResponse with the correct status code -- it works. I've tried no_cache decorators, etc.
2) Should there be something better baked into DjangoCMS to catch / handle Django handler events for 404 / 500 / others? I feel the answer is yes however I'm open to suggestions on a different approach than the one I'm using.

Jacob Rief

neskaityta,
2019-11-22 20:06:252019-11-22
kam: django CMS developers
Two things:

1) Not sure why the Django cache framework and DjangoCMS caches the page when setting the 404 directly (as shown in the comments above). I cannot find in the response.render where the status_code gets clobbered back to 200.  However clearly if I grab the content (not the headers or statuscode) and return that in a new HttpResponse with the correct status code -- it works. I've tried no_cache decorators, etc.
2) Should there be something better baked into DjangoCMS to catch / handle Django handler events for 404 / 500 / others? I feel the answer is yes however I'm open to suggestions on a different approach than the one I'm using.

Error pages typically are not served by the CMS, therefore django-CMS doesn't offer any special page inside its menu tree.
What you can do however, is to create a custom model using a PlaceholderField. Then create a view serving your error pages.
Inside the template used by that error-response view, refer to that model object using the given placeholder.
These are the advantages of that approach:
  1. You don't need to abuse any page from the tree in django-CMS. Error messages are separated from normal content.
  2. This allows your client to use the structure mode for editing the placeholder's content.
  3. By adding fields language and status_code to that model, you can customize what is rendered on any occasion in any language.
This btw. is an interesting use case and writing a reusable app for that purpose would make a lot of sense.

Peter Farrell

neskaityta,
2019-11-22 20:30:462019-11-22
kam: django-cms...@googlegroups.com
I've never thought about using a Placeholder field for this purpose. Thanks for the suggestion... I might have a crack at a reusable app this coming week.

--
Message URL: https://groups.google.com/d/msg/django-cms-developers/topic-id/message-id
Unsubscribe: send a message to django-cms-devel...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "django CMS developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-cms-devel...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/django-cms-developers/b746774e-8990-420b-9769-e9d7e413c733%40googlegroups.com.
Atsakyti visiems
Atsakyti autoriui
Persiųsti
0 naujų pranešimų