`context_instance=RequestContext(request)` 是否在 django 中使用 cookies

发布于 2024-10-07 01:22:17 字数 706 浏览 0 评论 0 原文

当我更改主页视图时:

def home(request):
    return render_to_response('homepage.html')

即使

def home(request):
    return render_to_response('homepage.html',context_instance=RequestContext(request))

我关闭网络浏览器(firefox),登录我网站的用户也将始终登录,

为什么context_instance=RequestContext(request)可以这样做这 ?它使用cookie吗?

这种状态会维持多久。

谢谢,

homepage.html 是:

{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please <a href="/account/login_view">login</a></p>
{% endif %}

when i change the homepage view:

def home(request):
    return render_to_response('homepage.html')

to

def home(request):
    return render_to_response('homepage.html',context_instance=RequestContext(request))

the user who login my site will always login even when i close the web Browser(firefox)

why context_instance=RequestContext(request) can do this ? Does it use cookies ?

how long time it will maintain this state .

thanks

the homepage.html is :

{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please <a href="/account/login_view">login</a></p>
{% endif %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2024-10-14 01:22:17

添加 RequestContext 根本不会更改用户的登录状态。你关于cookies的问题根本没有意义。 RequestContext 的作用是使某些变量可以在模板上下文中访问 - 其中,假设您启用了 auth 上下文处理器,则为 user 变量。

如果没有 RequestContext,用户仍处于登录状态,但您不会将 user 变量传递给上下文,因此您的 if 语句的计算结果为 False。与用户的实际状态完全无关。

Adding the RequestContext does not change the user's logged-in state at all. And your question about cookies makes no sense at all. What the RequestContext does is make certain variables accessible in the template context - among them, assuming you have the auth context processor enabled, is a user variable.

Without the RequestContext, the user is still logged in, but you don't pass the user variable to the context, so your if statement evaluates to False. Nothing to do with the user's actual status at all.

傲性难收 2024-10-14 01:22:17

如果您从 上下文处理器 将添加到模板中,在您的情况下,这包括 user< 中的当前用户对象/代码>。
如果删除它,则该变量不存在,但在模板内这不会引发异常,但会呈现未登录用户的 HTML!

用户信息存储在使用 cookie 的 会话 中。

If you add context_instance=RequestContext(request) context from the context processors you have defined in your settings.py will be added to the template, in your case this includes the current user object in user.
If you remove it, the variable doesn't exist, but inside the template this doesn't raise an exception but the HTML for the not-logged in user is rendered!

The user information is stored in a session, which uses cookies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文