本人写了一个轻量级的Flask程序。按逻辑,当用户填写表单的Username和Password字段后单击Log in按钮时,应闪现一条相关的flash消息,并且页面应被重定向到 /upload 页面(即upload视图函数)。但运行时消息闪现未出现,且页面并未重定向,而是不断停留在原页面(即index视图函数)(类似于刷新)。经本人检查,表单理论上定义应该通过了视图函数内置的检验。相关代码如下:
(app.py)(on windows 10)(Visual Studio Code)
from flask import Flask,session,request,render_template,flash,redirect,url_for
from forms import LoginForm,UploadForm
app = Flask(__name__)
app.secret_key='a62f2225bf70bfaccbc7f1ef2a397836717377de'
@app.route('/',methods=['GET','POST'])
def index():
form = LoginForm()
if request.method =='POST' and form.validate():
username = form.username.data
password = form.password.data
flash('Welcome! %s' % username) #to a teat
session['username'] = username #to a test
return redirect(url_for('upload')) # to a test
#FINAL: database operation:
# pass
return render_template('index.html',form=form)
@app.route('/upload',methods=['GET','POST'])
def upload():
form = UploadForm()
if request.method=='POST' and form.validate():
file = form.file.data
message = '' #prevent null error
message = form.message.data
#FINAL: database operation:
# pass
return render_template('upload.html',form=form,username=session.get('username'))
if __name__ == '__main__':
app.run(debug=1)
(forms.py)
from wtforms import Form,StringField,PasswordField,SubmitField,FileField, TextAreaField
from wtforms.validators import DataRequired,Length
class LoginForm(Form):
username = StringField('Username',validators=[DataRequired()])
password = PasswordField('Password',validators=[DataRequired()])
submit = SubmitField('Log In')
class UploadForm(Form):
file = FileField('Your File',validators=[DataRequired()])
message = TextAreaField('Other Message',validators=[Length(min=-1,max=150,message=None)])
submit = SubmitField('OK')
(index.html)
{% extends 'base.html' %}
{% block title %}Welcome!{% endblock %}
{% block content %}
<h1>Welcome! Please Log In</h1>
<form method="post">
{{form.csrf_token}}
<div class="form-group">
{{form.username.label}}<br>{{form.username(class='form-control')}}<br>
</div>
<div class="form-group">
{{form.password.label}}<br>{{form.password(class='form-control')}}<br>
</div>
{{form.submit(class='btn btn-primary')}}<br>
</form>
{% endblock %}
(upload.html)
{% extends 'base.html' %}
{% block title %}Upload{% endblock %}
{% block content %}
<h1>Hi, {{username}}</h1>
<form method="post">
{{form.csrf_token}}
<div class="form-group">
{{form.file.label}}<br>{{form.file(class='form-control')}}<br>
</div>
<div class="form-group">
{{form.message.label}}<br>{{form.message(class='form-control')}}<br>
</div>
{{form.submit(class='btn btn-primary')}}<br>
</form>
{% endblock %}
(base.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{%block title%}{%endblock%}</title>
<link rel="icon" type="image/x-icon" href="{{url_for('static',filename='favicon.ico')}}">
<link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.css')}}">
</head>
<body>
<main>
{% for message in get_flashed_messages() %}
<div class="alert">{{message}}</div>
{% endfor %}
{% block content %}{% endblock %}
</main>
</body>
<script src="{{url_for('static',filename='js/bootstrap.js')}}"></script>
</html>
问题截图:
单击前(用户名与密码是虚拟的)
单击后