在Django中,是否可以从自定义标签中访问当前的用户会话? [英] In Django, is it possible to access the current user session from within a custom tag?

查看:72
本文介绍了在Django中,是否可以从自定义标签中访问当前的用户会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django中编写一个自定义标签,它应该输出一个存储在用户会话中的值,但是我找不到一个方法可以在自定义标签功能中访问会话对象。有没有办法这样做,没有手动将会话对象分配给上下文变量?

解决方案

你应该可以添加您的settings.py文件中的请求上下文处理器:

  TEMPLATE_CONTEXT_PROCESSORS =(django.core.context_processors.auth,
django.core.context_processors.debug,
django.core.context_processors.i18n,
django.core.context_processors.media,
'django.core.context_processors .request',)

这将做与当前答案相同的事情,而不必添加自定义文件。


I am writing a custom tag in Django that should output a value stored in a user session, but I cannot find a way to access the session object from within a custom tag function. Is there any way to do this, without manually assigning the session object to a context variable?

解决方案

You should be able to add the request context processor in your settings.py file:

TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
'django.core.context_processors.request',)

This will do the same thing as the current answer, without having to add a custom file.

这篇关于在Django中,是否可以从自定义标签中访问当前的用户会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆