Django:从模板中访问会话变量?(Django: accessing session variables from within a template?)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
Django:从模板中访问会话变量?(Django: accessing session variables from within a template?)

如果我在Django中设置一个会话变量,就像:

request.session["name"] = "name"

有没有办法可以从模板中访问它,或者我必须从视图中检索它,然后将其传递给模板?

要求因为我有10个小的会话变量,我想在一个模板中访问,并将所有10从视图传递到模板可能会有点凌乱。

(我必须使用会话变量,因为它是一个HttpResponseRedirect,但将变量存储在数据库中是为我的目的而过度的)

所以 - 任何方式直接在模板中抓取会话变量?

If I set a session variable in Django, like:

request.session["name"] = "name"

Is there a way I can access it from within a template, or do I have to retrieve it from within a view, and then pass it to a template?

Asking because I have around 10 little session variables that I'd like to access within a template, and passing all 10 from the view to the template could get a bit messy.

(I have to use session variables because it's a HttpResponseRedirect, but storing the variables in a database is overkill for my purposes.)

So - any way to grab session variables directly within a template?

最满意答案

您需要将django.core.context_processors.request添加到模板上下文处理器中 。 那么你可以这样访问它们:

{{ request.session.name }}

如果您使用自定义视图,请确保传递了RequestContext实例。 从文档中取出的示例:

from django.shortcuts import render_to_response from django.template import RequestContext def some_view(request): # ... return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request))

更新2013年:根据我仍然收到的这个答案,人们仍然觉得它有帮助,在它最初写作三年多以后。 但请注意,尽管上述视图代码仍然有效,但是现在有一种简单的方式可以做到这一点。 render()是一个非常类似于render_to_response()的函数,但它自动使用RequestContext ,而不需要明确地传递:

from django.shortcuts import render def some_view(request): # ... return render(request, 'my_template.html', my_data_dictionary)

You need to add django.core.context_processors.request to your template context processors. Then you can access them like this:

{{ request.session.name }}

In case you are using custom views make sure you are passing a RequestContext instance. Example taken from documentation:

from django.shortcuts import render_to_response from django.template import RequestContext def some_view(request): # ... return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request))

Update 2013: Judging by the upvotes I'm still receiving for this answer, people are still finding it helpful, more than three years after it was originally written. Please note however, that although the view code above is still valid, nowadays there is a much simpler way of doing this. render() is a function very similar to render_to_response(), but it uses RequestContext automatically, without a need to pass it explicitly:

from django.shortcuts import render def some_view(request): # ... return render(request, 'my_template.html', my_data_dictionary)

更多推荐

本文发布于:2023-04-21 18:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/06a6190ee0e0590af5863f29550ab419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   模板   Django   accessing   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!