python - python 和 Django 请求上下文错误

  显示原文与译文双语对照的内容
0 0

我正在设置一个 Django 网站,传送到我那里,我遇到了一个奇怪的错误。 这只发生在调用下面的模板代码的页面上。 我已经尝试删除模板调用,应用程序返回 500个错误。 有没有人看到过这样的东西,或者可以能能够建议一种进一步疑难解答的方法? Ubuntu12,orca,MySQL,Mod_Python,python 2.7,Django 1.4

堆栈跟踪:


MOD_PYTHON ERROR

ProcessId: 8770
Interpreter: 'ip-10-245-86-33.ec2.internal'

ServerName: 'ip-10-245-86-33.ec2.internal'
DocumentRoot: '/home/public_html/site'

URI: '/location/index.html'
Location: '/'
Directory: None
Filename: '/home/public_html/site/apache/django.wsgi'
PathInfo: '/location/index.html'

Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'

Traceback (most recent call last):

 File"/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
 default=default_handler, arg=req, silent=hlist.silent)

 File"/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1229, in _process_target
 result = _execute_target(config, req, object, arg)

 File"/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1128, in _execute_target
 result = object(arg)

 File"/usr/local/lib/python2.7/dist-packages/django/core/handlers/modpython.py", line 180, in handler
 return ModPythonHandler()(req)

 File"/usr/local/lib/python2.7/dist-packages/django/core/handlers/modpython.py", line 158, in __call__
 response = self.get_response(request)

 File"/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 179, in get_response
 response = self.handle_uncaught_exception(request, resolver, sys.exc_info())

 File"/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
 return callback(request, **param_dict)

 File"/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 91, in _wrapped_view
 response = view_func(request, *args, **kwargs)

 File"/usr/local/lib/python2.7/dist-packages/django/views/defaults.py", line 33, in server_error
 return http.HttpResponseServerError(t.render(Context({})))

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render
 return self._render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
 return self.nodelist.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
 bit = self.render_node(node, context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 837, in render_node
 return node.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 123, in render
 return compiled_parent._render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
 return self.nodelist.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
 bit = self.render_node(node, context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 837, in render_node
 return node.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 123, in render
 return compiled_parent._render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
 return self.nodelist.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
 bit = self.render_node(node, context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 837, in render_node
 return node.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 62, in render
 result = block.nodelist.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 823, in render
 bit = self.render_node(node, context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 837, in render_node
 return node.render(context)

 File"/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1107, in render
 return func(*resolved_args, **resolved_kwargs)

 File"/home/public_html/site/asite/templatetags/meta_extras.py", line 35, in meta_url_reverse
 host = context["request"].get_host();

 File"/usr/local/lib/python2.7/dist-packages/django/template/context.py", line 54, in __getitem__
 raise KeyError(key)

KeyError: 'request'

模板代码:


{% block meta %}
 {% load meta_extras %}
 {% meta_title flatpage.title %}
 {% meta_description flatpage.content %}
 {% meta_url_reverse"asite_flatpage" flatpage.url %}
 {% block meta_image %}{% endblock %}
{% endblock %}

有问题的函数调用:


from django import template
from django.template.loader_tags import register
from django.core.urlresolvers import reverse_lazy
from django.template.context import Context

@register.simple_tag(takes_context=True)
def meta_url_reverse(context,args, pk=None):
 if pk is not None:
 url = reverse_lazy(args,args=[pk]);
 else:
 url = reverse_lazy(args);


 host = context["request"].get_host();
 if context["request"].is_secure():
 protocol ="https"
 else:
 protocol ="http"


 return"<link rel='canonical' href='{2}://{1}{0}'></link><meta property='og:url' content='{2}://{1}{0}'/>".format(url,host,protocol)

时间: 原作者:

0 0

错误是 KeyError 抱怨键 request 对上下文不可用。

确保你的TEMPLATE_CONTEXT_PROCESSORS 包含 django.core.context.processors.request ,或者在呈现模板的视图中显式将它的添加到上下文中。

==编辑= =

如果在设置中存在,那么你还需要确保视图本身返回 RequestContext 而不是 Context

原作者:
...