1

我有一个自定义上下文处理器。我正在获取每个请求的 url_name 以传递给全局模板。

def custom_context_processor(request=None, sender=None, **kwargs):
    url_name = resolve(request.path).url_name
    .....

当调试模式为 True 时一切正常。但是当它设置为 False 我得到 500 服务器错误。

经过一段时间的测试,我发现 i18n url 模式不适用于 debug=False。

例如,如果我写 localhost/ 它不会重定向到默认语言作为 localhost/en

但是如果我写 localhost/en django 没有给出错误并且按预期工作......

在 settings.py 我有:

ALLOWED_HOSTS = ['*']

我在模板文件夹中有 404.html 和 500.html 错误页面...

当我在 Django 中启用错误日志时,我得到:

  Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/handlers/base.py", line 156, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/handlers/base.py", line 224, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/Users/burakk/BurakWorks/Web/Shiftf5/Development/Eclipse/shiftf5/shiftf5/urls.py", line 64, in server_error
    response = render(request, "errors/index.html")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/shortcuts/__init__.py", line 49, in render
    context_instance = RequestContext(request, current_app=current_app)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/template/context.py", line 179, in __init__
    self.update(processor(request))
  File "/Users/burakk/BurakWorks/Web/Shiftf5/Development/Eclipse/shiftf5/shiftf5/web/context_processors.py", line 31, in shiftf5_web
    url_name = resolve(request.path)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/urlresolvers.py", line 440, in resolve
    return get_resolver(urlconf).resolve(path)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/core/urlresolvers.py", line 334, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})
Resolver404: {u'path': u'', u'tried': [[<RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>], [<RegexURLResolver <module 'mediaalbums.urls' from '/Users/burakk/BurakWorks/Web/Shiftf5/Development/Eclipse/shiftf5/mediaalbums/urls.pyc'> (None:None) ^shiftf5_mediaalbums/>], [<RegexURLResolver <module 'django.conf.urls.i18n' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/conf/urls/i18n.pyc'> (None:None) ^i18n/>], [<RegexURLResolver <module 'taggit_autosuggest.urls' from '/Users/burakk/BurakWorks/Web/Shiftf5/Development/Eclipse/shiftf5/taggit_autosuggest/urls.pyc'> (None:None) ^taggit_autosuggest/>], [<RegexURLResolver <module 'grappelli.urls' from '/Users/burakk/BurakWorks/Web/Shiftf5/Development/Eclipse/shiftf5/grappelli/urls.pyc'> (None:None) ^grappelli/>], [<RegexURLPattern None ^favicon\.ico$>], [<RegexURLResolver <module 'ckeditor.urls' from '/Users/burakk/BurakWorks/Web/Shiftf5/Development/Eclipse/shiftf5/ckeditor/urls.pyc'> (None:None) ^ckeditor/>], [<RegexURLResolver <module 'rosetta.urls' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_rosetta-0.7.1-py2.7.egg/rosetta/urls.pyc'> (None:None) ^rosetta/>], [<LocaleRegexURLResolver <RegexURLResolver list> (None:None) ^en/>], [<RegexURLPattern haystack_search_ml ^search/$>]]}
[13/Sep/2013 15:57:54] "GET / HTTP/1.1" 500 59
4

0 回答 0