2014-10-18 57 views
0

我用的燒瓶,燒瓶WTF與路徑和可選參數新的一頁,我在我的views.py文件下面的代碼:重定向到瓶和瓶,跆拳道不工作

from flask import render_template, flash, redirect, url_for 
from . import app, forms 

@app.route('/', methods=['GET', 'POST']) 
@app.route('/index', methods=['GET', 'POST']) 
def index(): 
    form = forms.HelpSearch() 
    if form.validate_on_submit(): 
     flash('Searched for: %s' % form.value.data) 
     redirect(url_for('help', form.value.data)) 
    return render_template('index.html', title='Index', form=form) 


@app.route('/help/<keyword>', methods=['GET', 'POST']) 
def help(keyword=None): 
    form = forms.HelpSearch() 
    if form.validate_on_submit(): 
     flash('Searched for: %s' % form.value.data) 
     redirect(url_for('help', keyword=form.value.data)) 

    # This is just some dummy data for testing my template 
    keywords = ['n', 'north'] 
    groups = ['movement'] 
    syntax = [ 
     {'cmd':"n", 'args': ''}, 
     {'cmd':'north', 'args': ''} 
    ] 
    content = 'Move north' 

    return render_template('show_help.html', 
          title=keyword, 
          form=form, 
          keywords=keywords, 
          groups=groups, 
          syntax=syntax, 
          content=content) 

我想要什麼,並期望,當某人在表單搜索字段中放置一些文本並點擊搜索按鈕時,它會返回該值,然後我重定向到相應的頁面,例如他們搜索foo並最終在/ help/foo。

不幸的是,表單驗證位的重定向沒有根據需要重定向。它似乎是重新加載當前頁面。

我知道表單正在獲取並返回數據,因爲flash調用顯示了正確的信息,例如, 'Searched for: foo'但當我通過關鍵字url_for該頁面,再次,只需重新加載。手動導航到/help/foo工作正常。

我測試過url_for正在工作,當我手動輸入關鍵字時,它會根據需要創建適當的路徑。 print url_for('help', keyword='foo')打印/help/foo

任何人都有任何想法,爲什麼它不是根據需要重定向?

編輯:運行於Heroku如果有人想看看究竟發生了什麼。

+0

您可以添加打印到控制檯的請求日誌嗎? – Miguel 2014-10-18 06:01:04

+0

@Miguel我正在手動運行它。但是,當我嘗試使用我的表單搜索時,這就是我所得到的:'127.0.0.1 - - [18/Oct/2014 03:42:50]「POST/HTTP/1.1」200 -'或GET。 – 2014-10-18 10:45:51

回答

0

我覺得你的問題是不返回任何東西

你可以檢查此:

def index(): 
    form = forms.HelpSearch() 
    if form.validate_on_submit(): 
     flash('Searched for: %s' % form.value.data) 
     return redirect(url_for('help', keyword=form.value.data)) 
    return render_template('index.html', title='Index', form=form) 
+0

表單不是?這只是它,數據就在那裏。閃光消息顯示頁面上搜索框中給出的輸入。 – 2014-10-18 16:17:11

0

的問題是你如何做你的重定向。取而代之的是:

redirect(url_for('help', keyword=form.value.data)) 

做到這一點:

return redirect(url_for('help', keyword=form.value.data)) 

redirect()功能不會引發異常像abort()做,只是返回一個Response對象,需要傳遞堆棧。

+0

聖...我不相信我遺漏了回報。我是一個白癡。 – 2014-10-19 04:58:56