Code Cracker
Code Cracker

Reputation: 159

Django Templates - How to use same block name inside other block?

i am creating a dynamic page for logged in and logged out users. If the user is authenticated then I want to show something different instead of my base template. Below is my code and you will surely understand after reading it. That's why i am trying this. If anyone has any idea how to do this task in django, your help will be really appreciated. The thing is that if i use same block name twice even inside a if condition in template it throw's an error that found same block name twice in the template. Is there is any way to use same block name twice???

{% extends 'gymwebapp/basic.html' %}


    {% if not user.is_authenticated %}
        {% block d-login %}
        <div id="dummy-right">
            <a href="{% url 'register' %}"><button class="btn">Sign Up</button></a>
            <a href="{% url 'login' %}"><button class="btn">Login</button></a>
        </div>
        {% endblock %}
        {% block login %}
        <div class="right">
            <a href="{% url 'register' %}"><button class="btn">Sign Up</button></a>
            <a href="{% url 'login' %}"><button class="btn">Login</button></a>
        </div>
        {% endblock %}
        {% block body %}
        <section class="form-area">
        <div class="form-container">

            <div class="form-heading">
                <h3>Login</h3>
            </div>
            <form action="login" method="POST">
                {% csrf_token %}
                
                <div class="form">
                    
                    
                    
                    <input type="email" placeholder="Email" id="email" name="email" maxlength="30" required></br>
                    <input type="password" placeholder="Password" id="pass" name="pass" maxlength="12" required></br>
                    
                    
                    
                    <button class="btn" id="fbtn" type="submit">Submit</button>
                </div>
            </form>
            </div>
        </section>
        {% endblock %}
        {% endif %}
    
        {% if user.is_authenticated %}
            {% block d-login %}
            <div id="dummy-right">
                <a href="{% url 'register' %}"><button class="btn">{{user.first_name}}</button></a>
                <a href="{% url 'logout' %}"><button class="btn">Logout</button></a>
            </div>
            {% endblock %}
            {% block login %}
            <div class="right">
                <a href="{% url 'register' %}"><button class="btn">{{user.first_name}}</button></a>
                <a href="{% url 'logout' %}"><button class="btn">Logout</button></a>
            </div>
            {% endblock %}
            {% block body %}
            <section class="msg">

                <ul class="messages">
                    
                    <li>{{ msg }}</li>

                </ul>
            </section>
            {% endblock %}
        
        {% endif %}

Upvotes: 1

Views: 351

Answers (1)

Jonatan
Jonatan

Reputation: 1242

I think you have some concepts confused. You use a block to define an area (block) in a base template. You can then populate the defined block by extending the template with the {% extends %} tag, and with by then using the block name name you defined in the parent template. Using the same block would mean that you would just override the the block the second time you use it. If you want to use the same code multiple times you can use the {% include %} tag, or by putting the code, which should be replicated, outside the block tag in the base/parent template.

Upvotes: 2

Related Questions