0

I'm setting up a Django site that was transferred over to me and I've hit a strange error. This is only happening on the pages that call the template code below. I've tried removing the template call and the application just returns a 500 error. Has anyone ever seen something like this or possibly be able to suggest a method of further troubleshooting? Ubuntu12, Apache2, MySQL, Mod_Python, Python 2.7, Django 1.4

Stack Trace:

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'

Template Code:

{% 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 %}

Offending Function Call:

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)
4

1 回答 1

1

该错误是KeyError抱怨密钥request对上下文不可用。

确保您的TEMPLATE_CONTEXT_PROCESSORSincludes django.core.context.processors.request,或者将其显式添加到呈现模板的视图中的上下文中。

== 编辑 ==

如果这存在于设置中,那么您还需要确保视图本身返回 aRequestContext而不是Context.

于 2013-10-08T20:30:25.523 回答