我从 Flask 开始,遇到了这个问题。我需要重定向到 url。当我尝试使用此代码时,效果很好:
@app.route('/fa')
def hello():
return redirect(url_for('foo'))
@app.route('/foo')
def foo():
return 'Hello Foo!'
但现在,我需要同样的,但需要一个渲染模板。这是代码,但后来我却是用户,烧瓶重定向并给出此错误:不允许方法
from flask import Flask, render_template, redirect, url_for
from forms import MyForm
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route("/signin", methods=("GET", "POST"))
def signin():
form = MyForm()
if request.method == 'POST':
if form.validate() == True:
return redirect(url_for('foo'))
elif request.method == 'GET':
return render_template("signin.html", form=form)
if __name__ == "__main__":
app.debug = True
app.run()
登录.html
{% block content %}
<h2>Sign In</h2>
<form method="POST" action="{{ url_for('signin') }}">
{{ form.hidden_tag() }}
{{ form.username.label }}
{{ form.username(size=20) }}
{{ form.password.label }}
{{ form.password(size=20) }}
{{ form.submit }}
</form>
{% endblock %}
我的表格.py
from flask_wtf import Form, TextField, PasswordField, DataRequired, SubmitField
class MyForm(Form):
username = TextField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
submit = SubmitField("Sign In")
def validate(self):
user = "franco"
if user == self.username:
return True
else:
return False
错误:
ValueError
ValueError: View function did not return a response
为什么??谢谢