2013-10-08 66 views
0

我設置了一個Django站點,它被轉移給我,並且我遇到了一個奇怪的錯誤。這隻發生在調用以下模板代碼的頁面上。我嘗試刪除模板調用,應用程序只返回500錯誤。有沒有人見過這樣的事情,或者可能會提出一種進一步的故障排除方法? Ubuntu12,的Apache2,MySQL和mod_python的,Python 2.7版,Django的1.4Python和Django請求上下文錯誤

堆棧跟蹤:

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) 
+1

django是否已經不具備urls的「reverse」功能? –

+1

有什麼錯誤?你爲什麼使用'reverse_lazy'?什麼是'flat page.url'? – jproffitt

+1

請在此處添加堆棧跟蹤,以便爲未來的訪問者設置上下文,同時請正確格式化您的代碼。 – zsquare

回答

1

錯誤是一個KeyError抱怨關鍵request是不可用的上下文。

確保您的TEMPLATE_CONTEXT_PROCESSORS包含django.core.context.processors.request,或者將其顯式添加到呈現模板的視圖中的上下文中。

== ==編輯

如果這是存在於設置,那麼你還需要確保該視圖本身返回一個RequestContext而非Context

+0

我檢查,它的作用: TEMPLATE_CONTEXT_PROCESSORS =( 「django.contrib.auth.context_processors.auth」, 「django.core.context_processors.debug」, 「django.core.context_processors.i18n」, 「Django的。 core.context_processors.media「, 」django.core.context_processors.request「, ) –