I get a invalid TemplateSyntaxError in Django for using the {% else %}-statement [

Viewed 179

Found the solution I was dumb. I thought I commented something, but apparently not. So the code was glitching on a 'commented out' {% else %}-statement. But still thanks.

My django-project (a tutorial https://www.youtube.com/watch?v=PtQiiknWUcI) keeps crashing on the basis that it doesn't accept my '{% else %}'-statement. I can't wrap my head around it.

<header class="header header--loggedIn">
    <div class="container">
      <a href="{% url 'home' %}" class="header__logo">
        <img src="assets/logo.svg" />
        <h1>StudyBuddy</h1>
      </a>
      <form class="header__search" method="GET" action="{% url 'home'%}">
        <label>
          <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
            <title>search</title>
            <path
              d="M32 30.586l-10.845-10.845c1.771-2.092 2.845-4.791 2.845-7.741 0-6.617-5.383-12-12-12s-12 5.383-12 12c0 6.617 5.383 12 12 12 2.949 0 5.649-1.074 7.741-2.845l10.845 10.845 1.414-1.414zM12 22c-5.514 0-10-4.486-10-10s4.486-10 10-10c5.514 0 10 4.486 10 10s-4.486 10-10 10z"
            ></path>
          </svg>
          <input name="q" placeholder="Search for rooms..." />
        </label>
      </form>
      <nav class="header__menu">
     

        <!-- Logged In -->
        {% if request.user.is_authenticated %}
            <div class="header__user">
            <a href="profile.html">
                <div class="avatar avatar--medium active">
                <img src="https://randomuser.me/api/portraits/men/37.jpg" />
                </div>
                <p>{{request.user.username}} <span>{{request.user.username}}</span></p>
            </a>
            <button class="dropdown-button">
                <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
                <title>chevron-down</title>
                <path d="M16 21l-13-13h-3l16 16 16-16h-3l-13 13z"></path>
                </svg>
            </button>
            </div>
        {% else %}
         <a href="{% url 'login' %}">
            <img src="./assets/avatar.svg" />
            <p>Login</p> 
         </a> 
        {% endif %}

        <div class="dropdown-menu">
          <a href="settings.html" class="dropdown-link"
            ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
              <title>tools</title>
              <path
                d="M27.465 32c-1.211 0-2.35-0.471-3.207-1.328l-9.392-9.391c-2.369 0.898-4.898 0.951-7.355 0.15-3.274-1.074-5.869-3.67-6.943-6.942-0.879-2.682-0.734-5.45 0.419-8.004 0.135-0.299 0.408-0.512 0.731-0.572 0.32-0.051 0.654 0.045 0.887 0.277l5.394 5.395 3.586-3.586-5.394-5.395c-0.232-0.232-0.336-0.564-0.276-0.887s0.272-0.596 0.572-0.732c2.552-1.152 5.318-1.295 8.001-0.418 3.274 1.074 5.869 3.67 6.943 6.942 0.806 2.457 0.752 4.987-0.15 7.358l9.392 9.391c0.844 0.842 1.328 2.012 1.328 3.207-0 2.5-2.034 4.535-4.535 4.535zM15.101 19.102c0.26 0 0.516 0.102 0.707 0.293l9.864 9.863c0.479 0.479 1.116 0.742 1.793 0.742 1.398 0 2.535-1.137 2.535-2.535 0-0.668-0.27-1.322-0.742-1.793l-9.864-9.863c-0.294-0.295-0.376-0.74-0.204-1.119 0.943-2.090 1.061-4.357 0.341-6.555-0.863-2.631-3.034-4.801-5.665-5.666-1.713-0.561-3.468-0.609-5.145-0.164l4.986 4.988c0.391 0.391 0.391 1.023 0 1.414l-5 5c-0.188 0.188-0.441 0.293-0.707 0.293s-0.52-0.105-0.707-0.293l-4.987-4.988c-0.45 1.682-0.397 3.436 0.164 5.146 0.863 2.631 3.034 4.801 5.665 5.666 2.2 0.721 4.466 0.604 6.555-0.342 0.132-0.059 0.271-0.088 0.411-0.088z"
              ></path>
            </svg>
            Settings</a
          >
          <a href="{% url 'logout' %}" class="dropdown-link"
            ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
              <title>sign-out</title>
              <path
                d="M3 0h22c0.553 0 1 0 1 0.553l-0 3.447h-2v-2h-20v28h20v-2h2l0 3.447c0 0.553-0.447 0.553-1 0.553h-22c-0.553 0-1-0.447-1-1v-30c0-0.553 0.447-1 1-1z"
              ></path>
              <path
                d="M21.879 21.293l1.414 1.414 6.707-6.707-6.707-6.707-1.414 1.414 4.293 4.293h-14.172v2h14.172l-4.293 4.293z"
              ></path>
            </svg>
            Logout</a
          >
        </div>
      </nav>
    </div>
  </header>

Django gives me a TemplateSyntaxError with the text: 'Invalid block tag on line 82: 'else'. Did you forget to register or load this tag?' The if-statement is there to control if the user is logged in. The 'else-statement' is there to show someone the inlog-page when not logged in.

I tried to google this. But for other people this 'error' comes up when they wrote there if-statement wrong. I hope that someone can help me. It's so frustrating.

I updated it and now it shows more code. It is a navbar.html that I'll import. But the error is on the navbar.html

This is the full django-error-report:

TemplateSyntaxError at /

Invalid block tag on line 82: 'else'. Did you forget to register or load this tag?

Request Method: GET Request URL: http://localhost:8000/ Django Version: 3.2.9 Exception Type: TemplateSyntaxError Exception Value:

Invalid block tag on line 82: 'else'. Did you forget to register or load this tag?

Exception Location: /home/milo/.local/lib/python3.8/site-packages/django/template/base.py, line 531, in invalid_block_tag Python Executable: /usr/bin/python Python Version: 3.8.10 Python Path:

['/home/milo/Bureaublad/code/python_django_site/studybud', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/milo/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']

Server time: Mon, 29 Nov 2021 15:53:03 +0000

{% extends 'main.html' %}

 {% block content %}
    <main class="layout layout--3">
      <div class="container">
        <!-- Topics Start -->
        <div class="topics">
          <div class="topics__header">
            <h2>Browse Topics</h2>
          </div>
      <ul class="topics__list">
        <li>
          <a href="/" class="active">All <span>553</span></a>
        </li>
        <li>
          <a href="/">Python <span>232</span></a>
        </li>
        <li>
          <a href="/">JavaScript <span>122</span></a>
        </li>
        <li>
          <a href="/">React <span>57</span></a>
        </li>
        <li>
          <a href="/">Database <span>90</span></a>
        </li>
      </ul>
      <a class="btn btn--link" href="topics.html">
        More
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
          <title>chevron-down</title>
          <path d="M16 21l-13-13h-3l16 16 16-16h-3l-13 13z"></path>
        </svg>
      </a>
    </div>
    <!-- Topics End -->

    <!-- Room List Start -->
    <div class="roomList">
      <div class="mobile-menu">
        <form class="header__search">
          <label>
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
              <title>search</title>
              <path
                d="M32 30.586l-10.845-10.845c1.771-2.092 2.845-4.791 2.845-7.741 0-6.617-5.383-12-12-12s-12 5.383-12 12c0 6.617 5.383 12 12 12 2.949 0 5.649-1.074 7.741-2.845l10.845 10.845 1.414-1.414zM12 22c-5.514 0-10-4.486-10-10s4.486-10 10-10c5.514 0 10 4.486 10 10s-4.486 10-10 10z"
              ></path>
            </svg>
            <input placeholder="Search for posts" />
          </label>
        </form>
        <div class="mobile-menuItems">
          <a class="btn btn--main btn--pill" href="#">Browse Topics</a>
          <a class="btn btn--main btn--pill" href="#">Recent Activities</a>
        </div>
      </div>
      <div class="roomList__header">
        <div>
          <h2>Study Room</h2>
          <p>7,439 Rooms available</p>
        </div>
        <a class="btn btn--main" href="create-room.html">
          <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
            <title>add</title>
            <path
              d="M16.943 0.943h-1.885v14.115h-14.115v1.885h14.115v14.115h1.885v-14.115h14.115v-1.885h-14.115v-14.115z"
            ></path>
          </svg>
          Create Room
        </a>
      </div>
      <div class="roomListRoom">
        <div class="roomListRoom__header">
          <a href="profile.html" class="roomListRoom__author">
            <div class="avatar avatar--small">
              <img src="https://randomuser.me/api/portraits/women/11.jpg" />
            </div>
            <span>@sulamita_ivy</span>
          </a>
          <div class="roomListRoom__actions">
            <span>5 days ago</span>
          </div>
        </div>
        <div class="roomListRoom__content">
          <a href="room.html">JavaScript made Simple</a>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur ducimus harum dolorem, obcaecati
            mollitia omnis quasi aut consequuntur enim itaque labore.
          </p>
        </div>
        <div class="roomListRoom__meta">
          <a href="room.html" class="roomListRoom__joined">
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
              <title>user-group</title>
              <path
                d="M30.539 20.766c-2.69-1.547-5.75-2.427-8.92-2.662 0.649 0.291 1.303 0.575 1.918 0.928 0.715 0.412 1.288 1.005 1.71 1.694 1.507 0.419 2.956 1.003 4.298 1.774 0.281 0.162 0.456 0.487 0.456 0.85v4.65h-4v2h5c0.553 0 1-0.447 1-1v-5.65c0-1.077-0.56-2.067-1.461-2.584z"
              ></path>
              <path
                d="M22.539 20.766c-6.295-3.619-14.783-3.619-21.078 0-0.901 0.519-1.461 1.508-1.461 2.584v5.65c0 0.553 0.447 1 1 1h22c0.553 0 1-0.447 1-1v-5.651c0-1.075-0.56-2.064-1.461-2.583zM22 28h-20v-4.65c0-0.362 0.175-0.688 0.457-0.85 5.691-3.271 13.394-3.271 19.086 0 0.282 0.162 0.457 0.487 0.457 0.849v4.651z"
              ></path>
              <path
                d="M19.502 4.047c0.166-0.017 0.33-0.047 0.498-0.047 2.757 0 5 2.243 5 5s-2.243 5-5 5c-0.168 0-0.332-0.030-0.498-0.047-0.424 0.641-0.944 1.204-1.513 1.716 0.651 0.201 1.323 0.331 2.011 0.331 3.859 0 7-3.141 7-7s-3.141-7-7-7c-0.688 0-1.36 0.131-2.011 0.331 0.57 0.512 1.089 1.075 1.513 1.716z"
              ></path>
              <path
                d="M12 16c3.859 0 7-3.141 7-7s-3.141-7-7-7c-3.859 0-7 3.141-7 7s3.141 7 7 7zM12 4c2.757 0 5 2.243 5 5s-2.243 5-5 5-5-2.243-5-5c0-2.757 2.243-5 5-5z"
              ></path>
            </svg>
            5.3k Joined
          </a>
          <p class="roomListRoom__topic">Python</p>
        </div>
      </div>
      <div class="roomListRoom">
        <div class="roomListRoom__header">
          <a href="profile.html" class="roomListRoom__author">
            <div class="avatar avatar--small active">
              <img src="https://randomuser.me/api/portraits/men/11.jpg" />
            </div>
            <span>@dennis_ivy</span>
          </a>
          <div class="roomListRoom__actions">
            <span>3 days ago</span>
          </div>
        </div>
        <div class="roomListRoom__content">
          <a href="room.html">100 Days of coding challenge</a>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur ducimus harum dolorem, obcaecati
            mollitia omnis quasi aut consequuntur enim itaque labore.
          </p>
        </div>
        <div class="roomListRoom__meta">
          <a class="roomListRoom__joined">
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
              <title>user-group</title>
              <path
                d="M30.539 20.766c-2.69-1.547-5.75-2.427-8.92-2.662 0.649 0.291 1.303 0.575 1.918 0.928 0.715 0.412 1.288 1.005 1.71 1.694 1.507 0.419 2.956 1.003 4.298 1.774 0.281 0.162 0.456 0.487 0.456 0.85v4.65h-4v2h5c0.553 0 1-0.447 1-1v-5.65c0-1.077-0.56-2.067-1.461-2.584z"
              ></path>
              <path
                d="M22.539 20.766c-6.295-3.619-14.783-3.619-21.078 0-0.901 0.519-1.461 1.508-1.461 2.584v5.65c0 0.553 0.447 1 1 1h22c0.553 0 1-0.447 1-1v-5.651c0-1.075-0.56-2.064-1.461-2.583zM22 28h-20v-4.65c0-0.362 0.175-0.688 0.457-0.85 5.691-3.271 13.394-3.271 19.086 0 0.282 0.162 0.457 0.487 0.457 0.849v4.651z"
              ></path>
              <path
                d="M19.502 4.047c0.166-0.017 0.33-0.047 0.498-0.047 2.757 0 5 2.243 5 5s-2.243 5-5 5c-0.168 0-0.332-0.030-0.498-0.047-0.424 0.641-0.944 1.204-1.513 1.716 0.651 0.201 1.323 0.331 2.011 0.331 3.859 0 7-3.141 7-7s-3.141-7-7-7c-0.688 0-1.36 0.131-2.011 0.331 0.57 0.512 1.089 1.075 1.513 1.716z"
              ></path>
              <path
                d="M12 16c3.859 0 7-3.141 7-7s-3.141-7-7-7c-3.859 0-7 3.141-7 7s3.141 7 7 7zM12 4c2.757 0 5 2.243 5 5s-2.243 5-5 5-5-2.243-5-5c0-2.757 2.243-5 5-5z"
              ></path>
            </svg>
            5.3k Joined
          </a>
          <p class="roomListRoom__topic">Python</p>
        </div>
      </div>
    </div>
    <!-- Room List End -->

    <!-- Activities Start -->
    <div class="activities">
      <div class="activities__header">
        <h2>Recent Activities</h2>
      </div>
      <div class="activities__box">
        <div class="activities__boxHeader roomListRoom__header">
          <a href="profile.html" class="roomListRoom__author">
            <div class="avatar avatar--small">
              <img src="https://randomuser.me/api/portraits/women/11.jpg" />
            </div>
            <p>
              @sulamita_ivy
              <span>5 days ago</span>
            </p>
          </a>
          <div class="roomListRoom__actions">
            <a href="#">
              <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
                <title>remove</title>
                <path
                  d="M27.314 6.019l-1.333-1.333-9.98 9.981-9.981-9.981-1.333 1.333 9.981 9.981-9.981 9.98 1.333 1.333 9.981-9.98 9.98 9.98 1.333-1.333-9.98-9.98 9.98-9.981z"
                ></path>
              </svg>
            </a>
          </div>
        </div>
        <div class="activities__boxContent">
          <p>replied to post “<a href="room.html">100 Days of code challenge!</a>”</p>
          <div class="activities__boxRoomContent">
            I’ll have to try this sometime. Really like this idea. Wanna talk about it? I ‘m....
          </div>
        </div>
      </div>

      <div class="activities__box">
        <div class="activities__boxHeader roomListRoom__header">
          <a href="profile.html" class="roomListRoom__author">
            <div class="avatar avatar--small active">
              <img src="https://randomuser.me/api/portraits/men/13.jpg" />
            </div>
            <p>
              @mr_spshuvo
              <span>5 days ago</span>
            </p>
          </a>
          <div class="roomListRoom__actions">
            <a href="#">
              <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
                <title>remove</title>
                <path
                  d="M27.314 6.019l-1.333-1.333-9.98 9.981-9.981-9.981-1.333 1.333 9.981 9.981-9.981 9.98 1.333 1.333 9.981-9.98 9.98 9.98 1.333-1.333-9.98-9.98 9.98-9.981z"
                ></path>
              </svg>
            </a>
          </div>
        </div>
        <div class="activities__boxContent">
          <p>replied to post “<a href="room.html">100 Days of code challenge!</a>”</p>
          <div class="activities__boxRoomContent">
            I’ll have to try this sometime. Really like this idea. Wanna talk about it? I ‘m....
          </div>
        </div>
      </div>
    </div>
    <!-- Activities End -->
  </div>
 </main>

{% endblock content %}

And main.html looks like this!

    <!DOCTYPE html>
{% load static %}
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="{% static 'styles/style.css' %}" />
    <title>StudyBuddy - Find study partners around the world!</title>
  </head>

  <body>

    {% include 'navbar.html' %}
    {% block content %}
    {% if messages %}
    <ul class="messages">
        {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
        {% endfor %}
    </ul>
    {% endif %}

    {% endblock content %}

    
   <script src="{% static 'js/script.js' %}"></script>
  </body>
</html>
1 Answers

Don't forget to load static if you have static files in your project.

{% load static %}

Related