[Flask] Error when transferring data to a template

David Nieder davidnieder at gmx.de
Thu Dec 29 11:40:34 EST 2016


On 28.12.2016 11:32, Andreas Dorfner wrote:
> Hello everyone,
>
> I've created a data-logger using Flask-SQLAlchemy.
>
> In the second part of the project, a webserver is set up to commit some
> of the database data to a website.
> Because there is an infinite loop at the logger file, another python
> file called webserver.py (code attached) is used to do so.
>
> The operation is planed as follows:
> 1. If the website is called, the login-window should appear.
>       ---> This part is working so far!
> 2. After a successful login, the home() function in the webserver.py
> file should redirect to the show_entries() function.
> 3. The show_entries() function read some data from the database; in this
> case the data with id=5 (this part is working as well);
>      and passes the entries to the show_entries.html template.
> 4. Depending on whether you are logged in or not, different data is
> shown on the website.
>
> The login is working well, but if I press the Login button, I get an
> error from the webbrowser. Does something going wrong by
> passing data to the template or is the template the problem (maybe the
> if-else-statement to be sure that the user is logged in)?
>

Hey Andreas.
One mistake in your code I could spot:
There is no 'entry' variable in you template just the list 'entries'.
So you probably forgot to iterate over the list:

{% if session.logged_in %}
   {% for entry in entries %}
     <h2>{{ entry.title }}</h2>
   {% endfor %}
{% else %}
   not logged in
{% endif %}


Cheers
David


> Thanks for your help!
> Kind regards,
> Andreas
>
>



More information about the Flask mailing list