G
G
gowa662015-10-02 01:33:58
Django
gowa66, 2015-10-02 01:33:58

Replacing a 404 template in Django?

How to exactly replace a template in Django 1.8?
url.py

from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin


urlpatterns = [
    url(r'^$', 'newsletter.views.home', name='home'),
    url(r'^contact/$', 'newsletter.views.contact', name='contact'),
]
handler404 = 'trydjango18.views.custom_404'
handler400 = 'views.custom_400'
handler500 = 'views.custom_500'

views.py
from django.shortcuts import render
def about(request):
  return render(request, "about.html", {})
def custom_404(request):
  return render(request, "404.html")
def custom_400(request):
  return render(request, "400.html")
def custom_500(request):
  return render(request, "500.html")

Mistake
Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/dj_static.py", line 83, in __call__
    return self.application(environ, start_response)
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
    response = self.get_response(request)
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 175, in get_response
    response = self.get_exception_response(request, resolver, 404)
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 90, in get_exception_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 267, in handle_uncaught_exception
    callback, param_dict = resolver.resolve_error_handler(500)
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 421, in resolve_error_handler
    return get_callable(callback), {}
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/gowa66/trydjango18/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 106, in get_callable
    mod = import_module(mod_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)

In the browser with an invalid link
A server error occurred.  Please contact the administrator.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zelsky, 2015-10-02
@zelsky

from django.shortcuts import render_to_response
from django.template import RequestContext


def handler404(request):
    response = render_to_response('404.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 404
    return response


def handler500(request):
    response = render_to_response('500.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

In your templates folder create 404.html and 500.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question