Skip to content Skip to sidebar Skip to footer

{% Extends "base.html" %} And {% Block Content %} Inside If Statement (django App)

In a Django template, I want to show different things to authenticated and unauthenticated users. Specifically, something like the following: {% if not user.is_authenticated %}

Solution 1:

{% extends var %} must always be the first tag in a template.

You can use an empty template and extend that for your purpose. Try inverting the logic and pulling out the block to the top level:

empty.html

{% block content %}
{% endblock %}

your file.html

{% extends user.is_authenticated|yesno:"base.html,empty.html" %}
    {% load humanize %}
    {% block content %}
    {% if not user.is_authenticated %}
    <divclass="margin">
    {{ group.topic }}
    </div>
{% else %}
    <divclass="margin">
        {{ group.topic }}
        <br><b>members:</b>
            {% for member in members %}
            <ahref="{% url 'profile' slug=member.username %}">{{ member.username }}</a>,&nbsp;
            {% endfor %}
        <hrsize=1><formaction="{% url 'private_group_reply' slug=unique %}"method="POST"enctype="multipart/form-data">
            {% csrf_token %}
            <inputtype="hidden"id="id_link"name="unique"class="hidden_id"value="{{ unique }}"><br>{{ form.image }}<br><br>{{ form.text }}<br><inputclass="button"type="submit"value="OK"id="id_submit"></form>
        {% for reply in replies %}
        {{ reply.writer }}: {{ reply.text }},{{ reply.submitted_on|naturaltime }}<br>
        {% endfor %}
{% endif %}
{% endblock %}

Solution 2:

I don't think extends could be used inside if block. Maybe you should consider use other way to do this. And it is reasonable, because you better not do logic thing in a template.

Other solutions will be like, for example, render different template file depends on if the user has logged in.

Post a Comment for "{% Extends "base.html" %} And {% Block Content %} Inside If Statement (django App)"