[Flask] How to use flask auth with blueprints

David Montgomery davidmontgomery at gmail.com
Tue Dec 15 17:56:53 EST 2015


Hi,

New to flask....and having issues with scaling the app.

So I created a bluiprint and decorated with @login_required. Auth, Users
etc are on my main views.py. If I remove @login_required my code works. But
if I add I am redirected to login page. Auth does work prior to moving the
code to a blueprint. So how do I import into my blueprint the objects
needed for auth?

Here is my blueprint

from flask import Flask, render_template,request,jsonify,Responsefrom
flask import make_response,session,redirect,url_for,gfrom
flask.ext.security import Securityfrom flask.ext.login import
login_user, logout_user, current_user, login_required,
UserMixin,confirm_login,fresh_login_requiredfrom flask_security import
auth_token_required, http_auth_requiredfrom passlib.apps import
custom_app_context as pwd_contextfrom flask.ext.httpauth import
HTTPBasicAuth # sudo pip install flask-httpauthfrom flask import
Blueprint
auth = HTTPBasicAuth()

delstack = Blueprint('/delstack/<stack_id>',
__name__)@delstack.route('/delstack/<stack_id>',
methods=['DELETE'])@login_requireddef delstack_route(stack_id):

    jd = {'status':'ok'}
    data = json.dumps(jd)
    resp = Response(data, status=200, mimetype='application/json')
    return resp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20151216/545cadb1/attachment.html>


More information about the Flask mailing list