获取错误(404,500等)django时重定向到现有视图

时间:2016-09-29 14:45:08

标签: python django redirect error-handling

当我的cookie会话到期时,它会出现500错误,我想将其重定向到登录。

我已添加了覆盖500处理程序的网址。

的myapp / urls.py

handler500 = 'myapp.login.views.signin'

但仅此一项并不奏效。

所以我尝试添加函数server_error:

的myapp / urls.py

handler500 = 'myapp.login.views.server_error'

的myapp /登录/ views.py

def server_error(request, template_name='templatename.html'):

    try:
        template = loader.get_template(template_name)
    except TemplateDoesNotExist:
        return http.HttpResponseServerError('<h1>Server Error (500)</h1>')
    return http.HttpResponseServerError(template.render(Context({})))

的myapp /登录/模板

templatename.html

错误是:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 64, in __call__
    return self.application(environ, start_response)
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
    response = self.get_response(request)
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 199, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 242, in handle_uncaught_exception
    callback, param_dict = resolver.resolve500()
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 403, in resolve500
    return self._resolve_special('500')
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 391, in _resolve_special
    return get_callable(callback), {}
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/user/ENV/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 101, in get_callable
    not module_has_submodule(import_module(parentmod), submod)):
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)


ImportError: No module named login

0 个答案:

没有答案