【问题标题】:Flask/Python 3 internal server error 500 when posting from html form从 html 表单发布时 Flask/Python 3 内部服务器错误 500
【发布时间】:2018-02-05 22:37:04
【问题描述】:

我是 Python 和 Flask 的新手,正在尝试设置一个非常基本的脚本,该脚本从表单中获取提交的信息并将其发布到新页面(我知道,很简单吧?)

我的成功有限,无法弄清楚这里的问题所在。当我在 python 文件中选择了 4 个表单字段中的 2 个时,它正在工作:

name=request.form['name']
age=request.form['age'] 

这可以正常工作,并且符合我的预期 - 呈现包含“姓名”和“年龄”的 output.html 页面

但一旦我尝试添加更多内容,我就会收到内部服务器错误 (500),即使我正在复制和粘贴完全相同的代码并且只更改变量(即“数字”和“感觉') - 在 .py 文件和输入和输出 html 文件中。

这是代码..

Python 代码:

(输入表单在 /input/ 页面上。“input_1”呈现 output.html 文件)

from flask import Flask, render_template, request, url_for, redirect
from dbconnect import connection
from flask_debugtoolbar import DebugToolbarExtension

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'kuywergiukrewgkbyuwe'
toolbar = DebugToolbarExtension(app)

app.config.update(TEMPLATES_AUTO_RELOAD = True)

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

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

@app.route('/input/', methods=["POST"])
def input_1():
    name=request.form['name']
    age=request.form['age']
    number=request.form['number']
    feeling=request.form['feeling']

    return render_template('output.html', name = name, age = age, number = number, feeling = feeling)

if __name__ == "__main__":
    app.run()

input.html 文件:

(包含输入表单)

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>devserver</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap.css") }}">
        <link rel="shortcut icon" href="{{ url_for("static", filename="favicon.ico") }}">
    </head>

<body>

<div class="container">
    <div class="col">

        <h2>Input form</h2>
        <br>
        <div class="form-group" >
            <form method="post" action="{{ url_for('input') }}">

            <label for="InputForm">Name</label>
            <input type="text" name="name" class="form-control"/>

            <label for="InputForm">Age</label>
            <input type="text" name="age" class="form-control"/>

            <label for="InputForm">Number</label>
            <input type="text" name="number" class="form-control"/>

            <label for="InputForm">Feeling</label>
            <input type="text" name="feeling" class="form-control"/>

            <br>
            <button type="submit" class="btn btn-primary">Submit</button>

            </form>
        </div>

    </div>

</div>

</body>

</html>

output.html 文件:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>devserver</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap.css") }}">
        <link rel="shortcut icon" href="{{ url_for("static", filename="favicon.ico") }}">
    </head>
    <body>

<div class="container">
    <div class="col">

        <h2>Output form</h2>
        <br>
        <div class="form-group" >
            <form>

            <h3>Output 1</h3>
            <P>Your name is = {{name}}</p>

            <h3>Output 2</h3>
            <P>Your age is = {{age}} </p>

            <h3>Output 3</h3>
            <P>Your number is = {{number}}</p>

            <h3>Output 4</h3>
            <P>Your feeling is = {{feeling}} </p>


            </form>
        </div>

    </div>

</div>

</body>

</html>

我不明白为什么它只适用于 2。当我注释掉以下内容时它工作正常:

@app.route('/input/', methods=["GET","POST"])
def input():
    name=request.form['name']
    age=request.form['age']
    #number=request.form['number']
    #feeling=request.form['feeling']

    return render_template('output.html', name = name, age = age) #number = number, feeling = feeling)

这可能很明显,但我就是看不到。

感谢您的帮助!

【问题讨论】:

标签: python html forms python-3.x flask


【解决方案1】:

当您必须生成 url 时,通常使用url_for。当我必须传递多个参数时,我宁愿不要使事情复杂化。我要做的就是这样:

<form method="post" action="/input">

在应用文件中:

@app.route('/input', methods=["POST"])
def input_1():
    name=request.form['name']
    age=request.form['age']
    number=request.form['number']
    feeling=request.form['feeling']

    return render_template('output.html', name = name, age = age, number = number, feeling = feeling)

但是,如果你真的想生成 url,那么就输入你想要生成 url 的函数并传递参数。

<form method="post" action={{url_for('input_1',name=name)}}>

然后像这样调用函数input_1

@app.route('/input/<name>') #you can add parameters as per your wish
def input_1(name):
    ...
    ...

【讨论】:

  • 如果不是恶意的,应该通过解释来支持拒绝投票。
猜你喜欢
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多