Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{% 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 %}
    <div class="margin">
    {{ group.topic }}
    </div>
{% else %}
    {% extends "base.html" %}
    {% load humanize %}
    {% block content %}
    <div class="margin">
        {{ group.topic }}
        <br>
        <b>members:</b>
            {% for member in members %}
            <a href="{% url 'profile' slug=member.username %}">{{ member.username }}</a>,&nbsp;
            {% endfor %}
        <hr size=1>
        <form action="{% url 'private_group_reply' slug=unique %}" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <input type="hidden" id="id_link" name="unique" class="hidden_id" value="{{ unique }}">
            <br>{{ form.image }}<br>
            <br>{{ form.text }}<br>
            <input class="button" type="submit" value="OK" id="id_submit">
        </form>
        {% for reply in replies %}
        {{ reply.writer }}: {{ reply.text }},{{ reply.submitted_on|naturaltime }}<br>
        {% endfor %}

    {% endblock %}
{% endif %}

In other words, I want to extend base.html only for authenticated users, for non-authenticated ones, I want to have a plain template.

I keep getting a TemplateSyntaxError: invalid block tag {% endif %}

How can I accomplish this?

The app in question is a legcy Django app, using Django 1.5.

like image 545
Hassan Baig Avatar asked Dec 28 '25 19:12

Hassan Baig


1 Answers

{% 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 %}
    <div class="margin">
    {{ group.topic }}
    </div>
{% else %}
    <div class="margin">
        {{ group.topic }}
        <br>
        <b>members:</b>
            {% for member in members %}
            <a href="{% url 'profile' slug=member.username %}">{{ member.username }}</a>,&nbsp;
            {% endfor %}
        <hr size=1>
        <form action="{% url 'private_group_reply' slug=unique %}" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <input type="hidden" id="id_link" name="unique" class="hidden_id" value="{{ unique }}">
            <br>{{ form.image }}<br>
            <br>{{ form.text }}<br>
            <input class="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 %}
like image 193
Sebastian Wozny Avatar answered Dec 30 '25 23:12

Sebastian Wozny