Python 烧瓶:Don';t重定向到Url
我从烧瓶开始,有这个问题。我需要重定向到url。当我尝试使用此代码时,效果良好:Python 烧瓶:Don';t重定向到Url,python,flask,Python,Flask,我从烧瓶开始,有这个问题。我需要重定向到url。当我尝试使用此代码时,效果良好: @app.route('/fa') def hello(): return redirect(url_for('foo')) @app.route('/foo') def foo(): return 'Hello Foo!' 但现在,我需要这个相同的,但与渲染模板。这是代码,但是我但是用户,flask重定向并给出这个错误:methodnotallowed from flask import Fl
@app.route('/fa')
def hello():
return redirect(url_for('foo'))
@app.route('/foo')
def foo():
return 'Hello Foo!'
但现在,我需要这个相同的,但与渲染模板。这是代码,但是我但是用户,flask重定向并给出这个错误:methodnotallowed
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()
signin.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 %}
错误:
ValueError
ValueError: View function did not return a response
为什么??谢谢问题不在于重定向(它工作正常),而在于http方法为POST且表单无效的情况。在这种情况下,
signin
功能没有有效响应,因此出现错误
@app.route("/signin", methods=("GET", "POST"))
def signin():
form = MyForm()
if request.method == 'POST':
if form.validate() == True:
return redirect(url_for('foo'))
else:
# If method is POST and form failed to validate
# Do something (flash message?)
return render_template("signin.html", form=form)
elif request.method == 'GET':
return render_template("signin.html", form=form)
为什么请澄清问题,并告诉我们什么不起作用。您的缩进非常不一致。如果这是你的文字代码,你可能有一个语法错误,因为你在顶层有一个
返回值
。对不起,是我的错误,我在这里写的代码不好。我编辑我的帖子你可以发布你的sign.html
的内容吗?你有一个bug:“username”==“franco”总是假的。
@app.route("/signin", methods=("GET", "POST"))
def signin():
form = MyForm()
if request.method == 'POST':
if form.validate() == True:
return redirect(url_for('foo'))
else:
# If method is POST and form failed to validate
# Do something (flash message?)
return render_template("signin.html", form=form)
elif request.method == 'GET':
return render_template("signin.html", form=form)