[Flask] Error when transferring data to a template

Andreas Dorfner andreas.dorfner at stud.hs-regensburg.de
Wed Dec 28 05:32:16 EST 2016


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)?

Thanks for your help!
Kind regards,
Andreas






---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus
-------------- next part --------------
#all the imports
import time
from flask import Flask, flash, redirect, render_template, request, session, abort
from flask_sqlalchemy import SQLAlchemy
import os
#--------------------------------------------------------------------------

#import the database models from models.py
from models import Phase1, Phase2, Phase3, PhaseTotal
#--------------------------------------------------------------------------


# configuration
USERNAME = 'xxxxxx'
PASSWORD = 'yyyyyyyy'
#---------------------------------------------------------------------------


# create my little application :)
app = Flask(__name__)
app.config.from_object(__name__)
#---------------------------------------------------------------------------


# read data with id 5 from the database and display it to the screen
#L1 = Phase1.query.filter_by(id=5).first()
#print "BP1"
#print(L1.Urms, L1.Qrms)
#---------------------------------------------------------------------------


# home function
@app.route('/')
def home():
    if not session.get('logged_in'):
	return render_template('login.html')
    else:
#	return render_template('show_entries.html')
	return redirect(url_for('show_entries'))
#---------------------------------------------------------------------------


#This function pass the entries as dicts to the show_entries.html template
#and return the broadcasted one.
@app.route('/show')
def show_entries():
    L1 = Phase1.query.filter_by(id=5).first()
    entries = [dict(title='Phase 1 with ID 5', Urms=L1.Urms, Irms=L1.Irms, Prms=L1.Prms)]
    flash('Data for Phase 1 is available!')	
    return render_template('show_entries.html', entries=entries)
#---------------------------------------------------------------------------


#Login function
@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif request.form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            flash('You were logged in')
            return home()
    return render_template('login.html', error=error)
#---------------------------------------------------------------------------


#Logout function
@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return home()
#---------------------------------------------------------------------------


#Run the file as a standalone application on IP address 192.168.7.2 (BBB)
#at Port 5000
if __name__ == '__main__':
    app.secret_key = os.urandom(12)
    app.run(host = '192.168.7.2', port=5000)
#---------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20161228/90485e43/attachment.html>


More information about the Flask mailing list