Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Build a Social Network with Flask Broadcasting Post Model

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'current_user'

Hi, everytime i try to access /register or /login i get this error: jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'current_user'

Anyone know why? it shows Hey when i go into the index site and works fine but as soon as i go over to one of those i get that error.

i tried to copy the exact same thing from the workspaces but i still get it.

REMMEMBER! I am doing this locally on my MacBook!

code from layout.html:

       <!--- Just showing the important things not the doctype header etc. -->


      <div class="row">

        <div class="grid-33">

          <a href="{{ url_for('index') }}" class="icon-logo"></a>

        </div>

        <div class="grid-33">

            <!-- Say Hi -->
            <h1>Hello {{ if current_user.is_authenticated() }} {{ current_user.username }}{% endif %}!</h1>

        </div>

        <div class="grid-33">

            <!-- Log in/Log out -->
            {{ if current_user.is_authenticated() }}
            <a href="{{ url_for('logout') }}" class="icon-power" title="Log out"></a>
            {% else %}
            <a href="{{ url_for('login') }}" class="icon-power" title="Log in"></a>
            <a href="{{ url_for('register') }}" class="icon-profile" title="Register"></a>
            {% endif %}

        </div>

      </div>

    </header>

    <!-- Flash messages -->
    {% with messages = get_flashed_messages(with_categories=True) %}
        {% if messages %}
            {% for category, message in messages %}
                <div class="notification {{ category }}">{{ message }}</div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <div class="row">

      <div class="main">

        <nav>

          <a href="{{ url_for('index') }}">All</a>

        </nav>

        {% block content %}{% endblock %}

code from register:

{% extends "layout.html" %} {% from 'macros.html' import render_field %}

{% block content %} <form method="POST" action="" class="form"> {{ form.hidden_tag() }} {% for field in form %} {{ render_field(field) }} {% endfor %} <button type="submit" id="submit">Register!</button> </form> {% endblock %}

code from login:

{% extends "layout.html" %} {% from 'macros.html' import render_field %}

{% block content %} <form method="POST" action="" class="form"> {{ form.hidden_tag() }} {% for field in form %} {{ render_field(field) }} {% endfor %} <button type="submit" id="submit">Register!</button> </form> {% endblock %}

2 Answers

Hi Mike

The error is pointing to line 40 which is the below

  <-!-- THIS IS MARKED {{ if current_user.is_authenticated() }} -->

  <-!-- IT SHOULD BE  {% if current_user.is_authenticated() % } in this case you need to check if a user is authenticated-->

  <-!-- In jinja 2 if you want to output a variable or print something you surround it with the 2 curly brackets. 
But to embed python code into html its the curly bracket with the percentage symbol. -->


 <-!-- And i am not aware about anything new in jinja 2 where you add a conditional statement using {{ }}.-->

to help you further, see below my layout file

<!DOCTYPE html>
<html class="no-js">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>{% block title %}TwoCans{% endblock %}</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="/static/css/normalize.min.css">
        <link rel="stylesheet" href="/static/css/main.css">

        <!--[if lt IE 9]>
            <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
            <script>window.html5 || document.write('<script src="/static/js/vendor/html5shiv.js"><\/script>')</script>
        <![endif]-->
    </head>
    <body>

        <header>

          <div class="row">

            <div class="grid-33">

              <a href="{{ url_for('index') }}" class="icon-logo"></a>

            </div>

            <div class="grid-33">

                <!-- Say Hi -->
                <h1>Hello{% if current_user.is_authenticated() %} {{ current_user.username }}{% endif %}!</h1>

            </div>

            <div class="grid-33">

                <!-- Log in/Log out -->
                {% if current_user.is_authenticated() %}
                <a href="{{ url_for('logout') }}" class="icon-power" title="Log out"></a>
                {% else %}
                <a href="{{ url_for('login') }}" class="icon-power" title="Log in"></a>
                <a href="{{ url_for('register') }}" class="icon-profile" title="Register"></a>
                {% endif %}

            </div>

          </div>

        </header>

        <!-- Flash messages -->
        {% with messages = get_flashed_messages(with_categories=True) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="notification {{ category }}">{{ message }}</div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <div class="row">

          <div class="main">

            <nav>

              <a href="{{ url_for('index') }}">All</a>
              {% if current_user.is_authenticated() %}
                <a href="{{url_for('post')}}" class="new"> create new post</a>
              {% endif %}

            </nav>

            {% block content %}{% endblock %}

          </div>

        </div>

        <footer>

          <div class="row">

            <p>A Social App built with Flask<br>by <a href="http://teamtreehouse.com">Treehouse</a></p>

          </div>

        </footer>

        <script src="/static/js/vendor/disTime.min.js"></script>
        <script src="/static/js/main.js"></script>

    </body>
</html>

Andreas in your file i just deleted the () in the user_authenticated and now it works! :)

Hi Mike

  <div class="grid-33">

            <!-- Say Hi -->
            <h1>Hello {{ if current_user.is_authenticated() }} {{ current_user.username }}{% endif %}!</h1>
            --> shouldn't this be !!!
            <h1>Hello {% if current_user.is_authenticated() %} {{ current_user.username }}{% endif %}!</h1>

  </div>

See if that fixes it, the error seems to be a if block statement not closed properly.

Hey Andreas, he says in the first episode of this 'season' that the new python api changed somethings so it has to be two {{ instead of %.

And i changed it and it didn't do anything, i still get the error.

it's like a whole website i get where it spits latest traceback and in there it says:

File "/Users/MikeHansen/PycharmProjects/zestymemes/templates/layout.html", line 40, in template </div>

        <div class="grid-33">

            <!-- Log in/Log out -->
            <-!-- THIS IS MARKED {{ if current_user.is_authenticated() }} -->
            <a href="{{ url_for('logout') }}" class="icon-power" title="Log out"></a>
            {% else %}
            <a href="{{ url_for('login') }}" class="icon-power" title="Log in"></a>
            <a href="{{ url_for('register') }}" class="icon-profile" title="Register"></a>          
           {% endif %}