2015-12-08 124 views
0

我試圖製作一個表單,將表單輸入保存爲URL查詢。但是,當我按下表單的提交按鈕時,我不知道爲什麼會發生這種情況。我確定它與redirect()或我的表單有關。Flask:NameError:未定義全局名稱'redirect'

site.py

from flask import Flask, render_template, request 

app = Flask(__name__) 

@app.route('/') 
def index(): 
    return render_template("about.html") 

@app.route('/hobbies/') 
def blog(): 
    return render_template("hobbies.html") 

@app.route('/about/') 
def about(): 
    return render_template("about.html") 

@app.route('/projects/', methods=['GET', 'POST']) 
def projects(): 
    return render_template("projects.html") 

@app.route('/dress') 
def dress(): 
    return render_template("dress.html") 

@app.route('/dress', methods=['GET', 'POST']) 
def my_form_post(): 
    min_bust = request.form['min_bust'] 
    max_bust = request.form['max_bust'] 
    min_waist = request.form['min_waist'] 
    max_waist = request.form['max_waist'] 
    min_length = request.form['min_length'] 
    max_length = request.form['max_length'] 
    return redirect(url_for('/dress/', 
          min_bust=min_bust, 
          max_bust=max_bust, 
          min_waist=min_waist, 
          max_waist=max_waist, 
          min_length=min_length, 
          max_length=max_length), code=302) 

@app.route('/base/') 
def base(): 
    return render_template("base.html") 

@app.errorhandler(404) 
def page_not_found(e): 
    return render_template("404.html"), 404 

@app.errorhandler(500) 
def internal_server_error(e): 
    return render_template('500.html'), 500 

if __name__ == '__main__': 
    app.run(debug=True) 

dress.html:

{% extends "base.html" %} 

{% block title %}Dress Finder{% endblock %} 

{% block menu %} 
<div class="menu"> 
    <a href="http://localhost:5000/hobbies/"><div class="button">Hobbies</div></a> 
    <a href="http://localhost:5000/about/"><div class ="button"> Katie</div></a> 
    <a href="http://localhost:5000/projects/"><div class="button" id="current_page">Projects</div></a> 
</div> 
{% endblock %} 

{% block content %} 
Dress Finder 
<br><br><br> 
Input desired Bust/Waist/Length measurements in inches and Dress Finder does the rest. 
<br><br> 
<form action="/dress" method="POST"> 
    <input type="text" name="min_bust" placeholder="Min Bust Size" class="input"> 
    to <input type="text" name="max_bust" placeholder="Max Bust Size" class="input"> 
    <br><br> 
    <input type="text" name="min_waist" placeholder="Min Waist" class="input"> 
    to <input type="text" name="max_waist" placeholder="Max Waist" class="input"> 
    <br><br> 
    <input type="text" name="min_length" placeholder="Min Length" class="input"> 
    to <input type="text" name="max_length" placeholder="Max Length" class="input"> 
    <br><br> 
    <input type="submit" name="my-form" value="     submit     " class="prettyButton"> 
</form> 
{% endblock %} 
+2

好吧,它說「重定向」沒有定義。你定義了它,還是從它定義的某個地方導入它? –

+0

如果它解決了您的問題,您應該接受該答案:) – Dan

回答

9

您需要更改此設置:

from flask import Flask, render_template, request 

這樣:

from flask import Flask, render_template, request, redirect 

redirect是由Flask庫提供的函數調用,但您必須將其導入到此文件中以使其可用。一般情況下爲See this

+0

謝謝。我以爲我在那裏。 – Thrynn

+1

這只是救了我的屁股。謝謝 – mmenschig

+0

@mmenschig非常歡迎! –