当前位置:  开发笔记 > 编程语言 > 正文

Flask:NameError:未定义全局名称"redirect"

如何解决《Flask:NameError:未定义全局名称"redirect"》经验,为你挑选了1个好方法。

我正在尝试创建一个将表单输入保存为URL查询的表单.但是,当我按下表单的提交按钮时,我不知道为什么会发生这种情况.我确定它与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 %}

{% endblock %}

{% block content %}
Dress Finder



Input desired Bust/Waist/Length measurements in inches and Dress Finder does the rest.

to

to

to

{% endblock %}

Nathaniel Fo.. 12

你需要改变这个:

from flask import Flask, render_template, request

对此:

from flask import Flask, render_template, request, redirect

redirect是Flask库提供的函数调用,但您必须将其导入此文件才能使用.一般情况见这.



1> Nathaniel Fo..:

你需要改变这个:

from flask import Flask, render_template, request

对此:

from flask import Flask, render_template, request, redirect

redirect是Flask库提供的函数调用,但您必须将其导入此文件才能使用.一般情况见这.

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有