【问题标题】:Why is my django template context processor not getting called为什么我的 django 模板上下文处理器没有被调用
【发布时间】:2009-10-06 19:29:46
【问题描述】:

我在设置自定义模板上下文时一定错过了一些东西,因为它永远不会被调用。

在设置中:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django_authopenid.context_processors.authopenid",
    "web.context_processors.my_hat",
)

在 web/context_processors.py 中

from libs.utils import get_hat, get_project, my_hats

print 'heloooo'

def my_hat(request):
    """Insert some additional information into the template context
    """
     
    import pdb
    pdb.set_trace()

    print 'hiiiiiiii'

    return {'hat': get_hat(request),
        'project': get_project(request),
        }

没有任何输出,django 处理视图并显示模板,而无需点击它。我错过了什么!?

感谢 Insin,我错过了一些东西:

在view.py中

return render_to_response(template, {
        'tasks': tasks,
    },
    context_instance=RequestContext(request))

在模板中:

  My current hat is {{hat}}

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您是否记得在渲染模板时使用RequestContext

    从 Django 1.3 开始,有一个新的快捷功能,render,默认使用RequestContext::

    return render(request, template, {
        'tasks': tasks,
    })
    

    【讨论】:

    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2020-10-29
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多