Problem with flag drawings and redirects for login

I have an example Flask app that basically works. Base URL ( /) does not require authentication; however, I have a resource in /auth/securedthat requires authentication.

The application works very well while the user is logged in; however, it throws werkzeug.routing.BuildError (BuildError: Could not build url for endpoint 'login' with values ['next']. Did you mean 'auth.login' instead?)when someone tries to access /auth/securedas an unauthenticated user, because I encoded it as follows:

@auth.route('/secured')
@ldap.login_required
def secured():
    return 'Login Success! {0}'.format(session)

I am using Flask Blueprints for my URL routing ... I assumed this should work ...

def create_app(config_name):
    from .main import main as main_blueprint
    from .auth import auth as auth_blueprint

    # Configure the flask instance...
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # Initialize the application...
    bootstrap.init_app(app)
    db.init_app(app)
    ldap.init_app(app)

    # Blueprint registration
    app.register_blueprint(main_blueprint)
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    return app

The application catalog is constructed as follows:

testapp/
 |
 + config.py
 + manage.py
 + app/
   |
   + __init__.py
   + auth/
     |
     + __init__.py
     + views.py
   + main/
     |
     + __init__.py
     + views.py

As far as I can tell, something went wrong in my project registration; however, I do not understand where the error is.

URL /auth/login, /auth/secured ?

+4
1

, .

- , LDAP_LOGIN_VIEW. login, 'login', , , auth.login :

LDAP_LOGIN_VIEW = 'auth.login'

, auth.login . , . , -simpleldap .

return redirect(url_for(current_app.config['LDAP_LOGIN_VIEW'], 
       next=request.path))

.

:

from flask import request
@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'],
                       request.form['password']):
            return log_the_user_in(request.form['username'])
        else:
            error = 'Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return render_template('login.html', error=error)
+2

All Articles