Python Flask:在返回的render_模板()或重定向()中包含HTML
我有一个登录表单,检查用户提供的用户名和密码是否返回数据库中的一行。如果有,则存储会话cookie,并将用户发送回索引页。如果没有,则再次将用户发送回登录页面 以下是相关代码的片段:Python Flask:在返回的render_模板()或重定向()中包含HTML,python,flask,sqlite,Python,Flask,Sqlite,我有一个登录表单,检查用户提供的用户名和密码是否返回数据库中的一行。如果有,则存储会话cookie,并将用户发送回索引页。如果没有,则再次将用户发送回登录页面 以下是相关代码的片段: cur.execute("SELECT COUNT(*) FROM users WHERE username = ? AND password = ?", (request.form['username'], request.form['password'])) data = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM users WHERE username = ? AND password = ?", (request.form['username'], request.form['password']))
data = cur.fetchone()[0]
# If given username and password returns a row, create the session
if data == 0:
return redirect(url_for('index'))
else:
session['username'] = request.form['username']
当用户再次被发送回登录页面(重定向(url\u for('index'))
)时,我希望在登录页面上包含一条消息,说明给定的用户名或密码无效
我有点不知道该怎么做 签出
flash
at。那应该是你想要的。只需在索引的模板中处理flash消息
这称为消息闪烁。完整的文档是,通过在视图端添加消息,然后在模板上呈现它们来完成,如下所示:
<!doctype html>
<title>My Application</title>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block body %}{% endblock %}
我的申请
{%with messages=get_flashed_messages()%}
{%if消息%}
{消息%中的消息为%s}
- {{message}}
{%endfor%}
{%endif%}
{%endwith%}
{%block body%}{%endblock%}
这正是我需要的。谢谢