Reputation: 105
I have created a Django project but I am using Apache as the webserver. Can anyone tell me how can I redirect an error code like 404 or 500 or 400 to a custom error html page instead of getting a standard error message on page in case an error was to occur ? I've tried the solutions available on the web but none seems to work
Upvotes: 1
Views: 3898
Reputation: 1
The exception missing in a view is a solution for this problem. Change:
def bad_request(request):
for:
def bad_request(request, exception):
Upvotes: 0
Reputation: 7717
I have a blog supported by django,in project's urls.py:
from django.conf.urls import handler400, handler403, handler404, handler500
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^admin/', admin.site.urls),
....
url(r'^rss/$', BlogFeed(), name='rss'),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='sitemap')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if not settings.DEBUG:
handler400 = 'common.views.bad_request'
handler403 = 'common.views.permission_denied'
handler404 = 'common.views.page_not_found'
handler500 = 'common.views.server_error'
common/views.py:
def bad_request(request):
context = {}
return render(request, '400.html', context, status=400)
def permission_denied(request):
context = {}
return render(request, '403.html', context, status=403)
def page_not_found(request):
context = {}
return render(request, '404.html', context, status=404)
def server_error(request):
context = {}
return render(request, '500.html', context, status=500)
Upvotes: 4
Reputation: 2344
from django.conf.urls import (handler403, handler404, handler500)
handler403 = 'my_app.views.permission_denied'
handler404 = 'my_app.views.page_not_found'
handler500 = 'my_app.views.server_error'
You can handle the requests like this.
Upvotes: 1