Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask working with wraps

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)
like image 519
bullet117 Avatar asked Dec 10 '13 19:12

bullet117


1 Answers

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.

like image 83
Martijn Pieters Avatar answered Sep 30 '22 16:09

Martijn Pieters