简体   繁体   English

Django CMS –在同一模板中为用户和访客显示不同的内容

[英]Django CMS – Show different content for users and guests in same template

I would like to have different content for users and guests in my home page's template using Django 1.9 and Django CMS 3.3.1 . 我想使用Django 1.9Django CMS 3.3.1在主页模板中为用户和访客提供不同的内容。

It could be acomplished by making subpages and showing the corresponding content in the ancestor based on authentication conditional, but that makes the page structure overly complicated. 通过基于认证条件创建子页面并在祖先中显示相应的内容可以完成此操作,但这会使页面结构过于复杂。

Is there an easy way of adding these placeholders straight to the template ? 是否有简单的方法将这些占位符直接添加到模板

I have tried this: 我已经试过了:

{% extends "base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute "page_title" %}{% endblock title %}

{% block content %}
    {% if not user.is_authenticated %}
        {% placeholder "guests" %}
    {% endif %}

    {% if user.is_authenticated %}
        {% placeholder "authenticated" %}
    {% endif %}

    {% placeholder "content" %}
{% endblock content %}

But as I am authenticated when I'm editing the content, I cannot access the guests placeholder. 但是,当我在编辑内容时通过身份验证时,无法访问guests占位符。

Try this: 尝试这个:

{% block content %}
    {% if request.toolbar.build_mode or request.toolbar.edit_mode %}

        {% placeholder "guests" %}
        {% placeholder "authenticated" %}

    {% else %}

        {% if not user.is_authenticated %}
            {% placeholder "guests" %}
        {% endif %}

        {% if user.is_authenticated %}
            {% placeholder "authenticated" %}
        {% endif %}

    {% endif %}

    {% placeholder "content" %}
{% endblock content %}

I have some experience with Django CMS but don't know if this will work. 我在Django CMS上有一些经验,但不知道这样是否可行。 The idea is to check if we're in edit mode by inspecting corresponding request variables. 这个想法是通过检查相应的请求变量来检查我们是否处于编辑模式。 See this answer . 看到这个答案


Update by @V-Kopio: 通过@ V-Kopio更新:

The answer given above works fine in practice but Django warns about dublicate placeholders. 上面给出的答案在实践中效果很好,但是Django警告有关双重占位符。 This can be avoided by combining the if and else blocks: 可以通过组合ifelse块来避免这种情况:

{% block content %}

    {% if not user.is_authenticated or request.toolbar.build_mode or request.toolbar.edit_mode %}
        {% placeholder "guests" %}
    {% endif %}

    {% if user.is_authenticated %}
        {% placeholder "authenticated" %}
    {% endif %}

    {% placeholder "content" %}
{% endblock content %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM