Задать вопрос

Замена шаблона 404 в Django?

Как ровно заменить шаблона в 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")


Ошибка
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)


В браузере при неверной ссылке
A server error occurred.  Please contact the administrator.
  • Вопрос задан
  • 2047 просмотров
Подписаться 3 Оценить Комментировать
Пригласить эксперта
Ответы на вопрос 1
zelsky
@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


В папке с темплейтами создайте 404.html и 500.html
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы