2015-06-19 108 views
1

我是新來的燒瓶。我正試圖將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請求失敗?

+1

在[調試模式](http://flask.pocoo.org/docs/0.10/quickstart/#debug-mode)中運行應用程序。 – nathancahill

+0

@nathancahill python run.py runserver --host 0.0.0.0 --debug這就是我所做的。 – LeonF

回答

1

問題不是url_for(),這是您使用wtf.quick_form()的方式。看看你的代碼生成的表單:

<form action="." method="post" class="form" role="form"> 

action="."行告訴瀏覽器採取提供的信息和其發佈到URL .。期間(.)表示「當前目錄」。所以發生了什麼是你點擊提交,然後你的瀏覽器發佈到localhost:5000/users/。 Flask將此請求發送到/users/,因此無法提供該請求,因爲/users/不是有效的URL。這就是爲什麼你會得到一個404.

幸運的是,這可以修復。在index.html,嘗試調用quick_form()和動作傳遞:

{{wtf.quick_form(form, action=url_for('react', user=user))}} 

現在,您的形式呈現:

<form action="/user/abc" method="post" class="form" role="form"> 

和瀏覽器就會知道的表單POST到/user/abc,這是一個有效URL,所以Flask會處理它。

您沒有發佈search.html的代碼,但嘗試將上述相同的邏輯應用於該模板;希望能解決這個問題!

+0

非常感謝!你的。我在哪裏可以找到'

'? 'url_for('react',user = user)'和'{{wtf.quick_form(form,action = url_for('react',user = user)}}''是一樣的嗎?事實上,我昨天寫了'react'視圖函數,它運行良好。今天我做了一些修改後失敗了。 – LeonF

+0

1.您可以在呈現的頁面上找到''元素。轉到http:// localhost:5000/user/abc,然後右鍵點擊頁面並點擊「查看頁面源代碼」(這可能被稱爲「查看源代碼」)。 2.總之,不,他們不一樣。 'url_for()'是一個Python函數,它返回一個字符串,它是函數的URL([see here](http://flask.pocoo.org/docs/0.10/api/#flask.url_for))。 'wtf.quick_form()'是一個Jinja2宏,它呈現一個表單([見這裏](https:// github。COM/MBR /燒瓶的自舉/斑點/主/ flask_bootstrap /模板/引導/ wtf.html#L131))。 –