[英]Getting an error: AttributeError: 'Flask' object has no attribute 'login_manager'
I am trying to run my flask app, but every time I load my index page, it gives me the error: AttributeError: 'Flask' object has no attribute 'login_manager'.我正在尝试运行我的 Flask 应用程序,但每次加载我的索引页面时,它都会给我错误:AttributeError: 'Flask' object has no attribute 'login_manager'。 It works before I put in this specific code在我输入这个特定的代码之前它可以工作
bp = flask.Blueprint("bp", __name__, template_folder="./build")
@bp.route('/index')
@login_required
def index():
# TODO: insert the data fetched by your app main page here as a JSON
DATA = {"your": "data here"}
data = json.dumps(DATA)
return flask.render_template(
"index.html",
data=data,
)
app.register_blueprint(bp)
This is my current code where it does work这是我当前可以工作的代码
@app.route("/index", methods=["GET", "POST"])
def index():
global current_user
if not current_user:
return flask.redirect(flask.url_for("login"))
if flask.request.method == "GET":
track_name, genius_link, track_artist, track_image, track_url = render()
# If user has no favorite artists, redirect back to profile.
if track_name == None:
return flask.redirect(flask.url_for("profile"))
return flask.render_template(
"index.html",
variable=track_name,
variable1=genius_link,
variable2=track_artist,
variable3=track_image,
variable4=track_url,
)
else:
valid_artist = validate_and_insert_artist(flask.request.form["artistId"])
if not valid_artist:
return flask.render_template("index.html", error=True)
else:
track_name, genius_link, track_artist, track_image, track_url = render()
# If user has no favorite artists, redirect back to profile.
if track_name == None:
return flask.redirect(flask.url_for("profile"))
return flask.render_template(
"index.html",
variable=track_name,
variable1=genius_link,
variable2=track_artist,
variable3=track_image,
variable4=track_url,
)
I am not sure why as soon as I put in the blueprint code, it stops working and gives me that error我不知道为什么我一输入蓝图代码,它就停止工作并给我那个错误
This is my login.html这是我的 login.html
@app.route("/login", methods=["GET", "POST"])
def login():
global current_user
if current_user:
return flask.redirect(flask.url_for("profile"))
if flask.request.method == "GET":
return flask.render_template("login.html")
if flask.request.method == "POST":
username = flask.request.form["username"]
cursor.execute(
"SELECT user_name FROM public.users WHERE user_name = %s", [username]
)
results = cursor.fetchall()
if len(results) != 0: # if a user exists, "log" them in
current_user = username
return flask.redirect(flask.url_for("profile"))
else:
return flask.render_template("login.html", error=True)
You need to read the Flask documentation for @login_required .您需要阅读@login_required的 Flask 文档。 As soon as you've added a method that requires the user to be logged in, you need to provide a method by which the user can log in.一旦添加了需要用户登录的方法,就需要提供用户可以登录的方法。
Or perhaps you just want to delete the @login_required
?或者您只是想删除@login_required
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.