在模板标签、django 之间传递上下文

发布于 2024-08-26 08:35:11 字数 685 浏览 6 评论 0原文

我正在使用 django 创建一个网络应用程序。

我在加载 templatetag 的位置创建了一个模板。在此 templatetag 中,我加载另一个 templatetag。我从模板将上下文传递给第一个 templatetag,但第二个 templatetag 中的上下文不可用(在第一个 templatetag 内) - 请参阅以下。

我希望这是有道理的,并且你们中的一个人能找到答案。


模板片段:

{% load templatetags %}
{% some_tag argument %}

some_tag Templatetag:

{% load templatetags %}
{% some_other_tag another_argument %}

some_other_tag Templatetag:

在此 templatetag 中,我尝试访问上下文以获取用户信息,即使用

request = context['request']
request.user

I am using django to create a web-application.

I have created a template in where I load a templatetag. In this templatetag I load another templatetag. From the template I pass context to the first templatetag, but the context is not available from the second templatetag (inside the first templatetag) - see below.

I hope this makes sense, and that one of you have the answer.


Template snippit:

{% load templatetags %}
{% some_tag argument %}

some_tag Templatetag:

{% load templatetags %}
{% some_other_tag another_argument %}

some_other_tag Templatetag:

In this templatetag I am trying to access context to get user info i.e. using

request = context['request']
request.user

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

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

发布评论

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

评论(1

黯然 2024-09-02 08:35:11

不要忘记子模板的上下文 - 以及第二个模板标记 - 是从第一个模板标记函数返回的任何内容。因此,您需要确保请求对象包含在您返回的字典中。

Don't forget that the context to the subtemplate - and hence to the second template tag - is whatever is returned from the first template tag function. So you'll need to ensure that the request object is included in the dictionary you return there.

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