Trying to setup a login page with Python and Flask and getting an Error:
(line 33 is the with @login_required
)
Traceback (most recent call last):
File "routes.py", line 33, in <module>
@login_required
File "/home/pi/FlaskTutorial/local/lib/python2.7/site-packages/flask/app.py", line 1013, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "/home/pi/FlaskTutorial/local/lib/python2.7/site-packages/flask/app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/home/pi/FlaskTutorial/local/lib/python2.7/site-packages/flask/app.py", line 942, in add_url_rule
endpoint = _endpoint_from_view_func(view_func)
File "/home/pi/FlaskTutorial/local/lib/python2.7/site-packages/flask/helpers.py", line 60, in _endpoint_from_view_func
assert view_func is not None, 'expected view func if endpoint ' \
AssertionError: expected view func if endpoint is not provided.
Here is the code I am using, I can't seem to find the problem... Any ideas?
from flask import *
from functools import wraps
app = Flask(__name__)
app.secret_key = 'secret key' #Use a random key generator
@app.route('/')
def home():
return render_template('home.html')
@app.route('/welcome')
def welcome():
return render_template('welcome.html')
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('log'))
return wrap
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect (url_for('log'))
@app.route('/hello')
@login_required
def hello():
return render_template('hello.html')
@app.route('/log', methods=['GET', 'POST'])
def log():
error = None
if request.method =='POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invaled Credentials. Please try again.'
else:
session['logged_in'] = True
return redirect(url_for('hello'))
return render_template('log.html', error=error)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000, debug=True)
You indented the return wrap
line too far, now your decorator returns a None
value. Unindent the last line:
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('log'))
return wrap
The exception points to the @login_required
line because the next decorator, @app.route('/hello')
throws the exception as it is applied to the output of @login_required
. The AssertionError
exception is thrown explicitly because the route decorator was passed a None
value for the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With