Skip to content

Instantly share code, notes, and snippets.

@Owanesh
Forked from revolunet/views.py
Last active April 20, 2020 16:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Owanesh/d70c2f8b4a349780016cfd97d056c9ec to your computer and use it in GitHub Desktop.
Save Owanesh/d70c2f8b4a349780016cfd97d056c9ec to your computer and use it in GitHub Desktop.
Create your custom error pages in Django, and use your context.
from django.conf.urls import handler404, handler500
handler500 = website_views.server_error
handler404 = website_views.page_not_found
from django.http import HttpResponseServerError, HttpResponseNotFound
from django.template import loader
def page_not_found(request, template_name='website/404.html'):
t = loader.get_template(template_name)
context = {
'your_var' : True,
'request' : request,
}
return HttpResponseNotFound(t.render(context))
def server_error(request, template_name='website/500.html'):
t = loader.get_template(template_name)
context = {
'your_var' : True,
'request' : request,
}
return HttpResponseServerError(t.render(context))
@Owanesh
Copy link
Author

Owanesh commented Mar 25, 2017

How to use

  1. Set DEBUG = False into settings.py
  2. Run server using python manage.py runserver --insecure

Use the --insecure option to force serving of static files with the staticfiles app even if the DEBUG setting is False.
https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/#runserver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment