我是新來的燒瓶。我正試圖將post/redirect/get模式應用到我的程序中。這是我做的。如何檢測POST請求(燒瓶)上的錯誤?
在index.html的
{% block page_content %}
<div class="container">
<div class="page-header">
<h1>Hello, {% if user %} {{ user }} {% else %} John Doe {% endif %}: {% if age %} {{ age }} {% else %} ?? {% endif %}</h1>
</div>
</div>
{% if form %}
{{wtf.quick_form(form)}}
{% endif %}
{% endblock %}
在views.py
class NameForm(Form):
age = DecimalField('What\'s your age?', validators=[Required()])
submit = SubmitField('Submit')
''''''
@app.route('/user/<user>', methods=['GET', 'POST'])
def react(user):
session['user'] = user
form = NameForm()
if form.validate_on_submit():
old_age = session.get('age')
if old_age != None and old_age != form.age.data:
flash('age changed')
session['age'] = form.age.data
return redirect(url_for('react', user = user))
return render_template('index.html', user = user, age = session.get('age'), form = form, current_time = datetime.utcnow())
的GET請求,當我打開xxxx:5000/user/abc
被處理好。但是,POST請求失敗。我收到一個404錯誤。我認爲url_for
函數可能會給redirect
一個錯誤的值。我如何檢查url_for
返回的值?
當我嘗試使用數據庫時,出現了405錯誤。這一次我不知道。
@app.route('/search', methods=['GET', 'POST'])
def search():
form = SearchForm() # a StringField to get 'name' and SubmitField
if form.validate_on_submit():
person = Person.query.filter_by(name = form.name.data) # Person table has two attributes 'name' and 'age'
if person is None:
flash('name not found in database')
else:
session['age'] = person.age
return redirect(url_for('search'))
return render_template('search.html', form = form, age = session.get('age'), current_time = datetime.utcnow())
有沒有一種方便的方式來調試POST請求失敗?
在[調試模式](http://flask.pocoo.org/docs/0.10/quickstart/#debug-mode)中運行應用程序。 – nathancahill
@nathancahill python run.py runserver --host 0.0.0.0 --debug這就是我所做的。 – LeonF