【问题标题】:User is not being passed to template TEMPLATE_CONTEXT_PROCESSORS用户未传递给模板 TEMPLATE_CONTEXT_PROCESSORS
【发布时间】:2014-06-30 16:31:30
【问题描述】:

关注docs

如果 TEMPLATE_CONTEXT_PROCESSORS 包含此处理器,每个 RequestContext 将包含这些变量:

user – 一个 auth.User 实例,代表当前登录的用户 (或 AnonymousUser 实例,如果客户端未登录)。烫发—— django.contrib.auth.context_processors.PermWrapper 的一个实例, 表示当前登录用户拥有的权限。

我将这些行添加到settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
)

如下渲染我的模板

def profile_view(request):
    return render_to_response('profile.html', { 'profile' : 1 })

我的印象是请求上下文和用户会自动传递。不是这样,还是我应该明确地将context_instance=template.RequestContext(request)) 添加到每个视图中?

谢谢。

【问题讨论】:

    标签: django


    【解决方案1】:

    是的,如果您想使用 render_to_response 并且希望模板上下文数据可用,您应该传入一个 RequestContext 实例。

    或者,如果您使用django.shortcuts.render 呈现您的页面,RequestContext 将被自动注入。

    您可能会发现这个答案也很有用:Django - what is the difference between render(), render_to_response() and direct_to_template()?

    【讨论】:

    • 酷,不知道。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2019-01-03
    • 1970-01-01
    • 2017-12-25
    • 2019-02-15
    • 2021-09-10
    • 1970-01-01
    相关资源
    最近更新 更多