django 片段缓存仅适用于匿名用户

发布于 2024-11-02 18:34:54 字数 390 浏览 12 评论 0 原文

我想为匿名用户使用 django 片段缓存,但为经过身份验证的用户提供新鲜数据。这似乎工作正常:

{% if user.is_anonymous %}

    {% load cache %}
    {% cache 300 "my-cache-fragment" %}
        <b>I have to write this out twice</b>
    {% endcache %}

{% else %}

    <b>I have to write this out twice</b>

{% endif %}

唯一的问题是我必须重复要缓存的 html。除了将其包含在其中之外,是否有一些聪明的方法可以解决此问题?谢谢。

I want to use django fragment caching for anonymous users, but give authenticated users fresh data. This seems to work fine:

{% if user.is_anonymous %}

    {% load cache %}
    {% cache 300 "my-cache-fragment" %}
        <b>I have to write this out twice</b>
    {% endcache %}

{% else %}

    <b>I have to write this out twice</b>

{% endif %}

The only problem is that I have to repeat the html to be cached. Is there some clever way around this, other than putting it in an include? Thanks.

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

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

发布评论

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

评论(4

以歌曲疗慰 2024-11-09 18:34:54

尝试将经过身份验证的用户的缓存超时设置为零。

视图.py:

context = {
    "cache_timeout": 300 if request.user.is_anonymous() else 0,
}

模板:

{% load cache %}
{% cache cache_timeout "my-cache-fragment" %}
    <b>I have to write this only once</b>
{% endcache %}

Try setting the cache timeout to zero for authenticated users.

views.py:

context = {
    "cache_timeout": 300 if request.user.is_anonymous() else 0,
}

Template:

{% load cache %}
{% cache cache_timeout "my-cache-fragment" %}
    <b>I have to write this only once</b>
{% endcache %}
我很OK 2024-11-09 18:34:54
{% with cache_timeout=user.is_staff|yesno:"0,300" %}
    {% cache cache_timeout cacheidentifier user.is_staff %}
            your content here
    {% endcache %}
{% endwith %}
{% with cache_timeout=user.is_staff|yesno:"0,300" %}
    {% cache cache_timeout cacheidentifier user.is_staff %}
            your content here
    {% endcache %}
{% endwith %}
逆蝶 2024-11-09 18:34:54

不确定我理解这个问题...

{% load cache %}
{% cache 300 "my-cache-fragment" %}
    <b>I have to write this out twice</b>
{% endcache %}

{% if not user.is_anonymous %}
    <b>And this is the extra uncached stuff for authenticated users</b>
{% endif %}

Not sure I understand the problem...

{% load cache %}
{% cache 300 "my-cache-fragment" %}
    <b>I have to write this out twice</b>
{% endcache %}

{% if not user.is_anonymous %}
    <b>And this is the extra uncached stuff for authenticated users</b>
{% endif %}
橘味果▽酱 2024-11-09 18:34:54

您可以通过将额外参数传递给 cache 标记来指定缓存,例如:

{% cache 500 sidebar request.user.is_anonymous %}

检查 此处了解更多信息...
但这也将为登录用户缓存数据......

可能你必须编写一个自定义模板标签。您可以首先检查现有的 cache 标记,然后根据该代码创建自定义标记。但不要忘记,django 缓存非常强大且复杂(比如在模板缓存中支持不同的语言)。

You can specify caching with passing extra parameters to cache tag like:

{% cache 500 sidebar request.user.is_anonymous %}

Check here for more info...
But this will also cache data for logged-in users too...

Probably you have to write a custom template tag. You can start by inspecting existing cache tag and create a custom tag based on that code. But do not forget, django caching is quite strong and complex(like supporting different languages in template caching).

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