消息闪烁的烧瓶(Message Flashing flask)

编程入门 行业动态 更新时间:2024-10-27 03:34:39
消息闪烁的烧瓶(Message Flashing flask)

我已经设置了一个烧瓶网络应用程序,目前我的代码已经设置好,以便当用户注册时,他们的详细信息会保存到数据库中,使用SQLite和SQLAlchemy进行设置。 在创建新用户的那一刻 - 它在新页面上return '<h1>New user has been created!</h1>' ,但我希望将其显示在同一个html页面上。

我查看了在线和Stack Overflow消息闪烁的示例,但不知道如何使用我的代码设置方式进行设置。 我在app.py文件和signup.html模板中包含了注册和登录部分以重现此问题。

在app.py文件中注册部分

@app.route('/signup/', methods=['GET', 'POST']) def signup(): form = RegisterForm() if form.validate_on_submit(): hashed_password = generate_password_hash(form.password.data, method='sha256') new_user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(new_user) db.session.commit() return '<h1>New user has been created!</h1>' return render_template('signup.html', form=form, errors=form.errors.items())

signup.html模板

{% block body_content %} <div class="container"> <form method="POST" action="/signup"> {{ form.csrf_token }} <div class="form-group"> {{ form.username }} </div> <div class="form-group"> {{ form.email }} </div> <div class="form-group"> {{ form.password }} </div> <button type="submit" class="btn btn-primary">Sign Up</button> </form> <div class="errors"> {% for field, err in errors %} <p>{{field}} : {{err|join(', ')}}</p> {% endfor %} </div> </div> {% endblock %}

app.py文件的登录部分

@app.route('/login/', methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user: if check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) return redirect(url_for('dashboard')) flash("Invalid username or password") return render_template('login.html', form=form, errors=form.errors.items())

I have a flask web app set up and currently my code has been set up so that when users sign up their details are saved to a database, set up using SQLite and SQLAlchemy. At the moment when the new user is created — it returns return '<h1>New user has been created!</h1>' on a new page, but I'm looking to get it to display on the same html page.

I have looked at examples of message flashing online and Stack Overflow, but not sure how to set it up with the way my code is set up. I have included the signup and login sections in app.py file and signup.html template to reproduce the problem.

Signup section in app.py file

@app.route('/signup/', methods=['GET', 'POST']) def signup(): form = RegisterForm() if form.validate_on_submit(): hashed_password = generate_password_hash(form.password.data, method='sha256') new_user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(new_user) db.session.commit() return '<h1>New user has been created!</h1>' return render_template('signup.html', form=form, errors=form.errors.items())

signup.html Template

{% block body_content %} <div class="container"> <form method="POST" action="/signup"> {{ form.csrf_token }} <div class="form-group"> {{ form.username }} </div> <div class="form-group"> {{ form.email }} </div> <div class="form-group"> {{ form.password }} </div> <button type="submit" class="btn btn-primary">Sign Up</button> </form> <div class="errors"> {% for field, err in errors %} <p>{{field}} : {{err|join(', ')}}</p> {% endfor %} </div> </div> {% endblock %}

Login section of app.py File

@app.route('/login/', methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user: if check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) return redirect(url_for('dashboard')) flash("Invalid username or password") return render_template('login.html', form=form, errors=form.errors.items())

最满意答案

在你的html中添加这段代码:

{% for message in get_flashed_messages() %} <div class=flash>{{ message }}</div> {% endfor %}

这在你的py:

from flask import flash

而不是

return "<h1>...

把这个:

flash("some text to flash")

Add this code in your html:

{% for message in get_flashed_messages() %} <div class=flash>{{ message }}</div> {% endfor %}

And this in your py:

from flask import flash

and instead of

return "<h1>...

Put this:

flash("some text to flash")

更多推荐

本文发布于:2023-07-04 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烧瓶   消息   Message   Flashing   flask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!