使用Flask Python加载选择字段

问题描述 投票:0回答:1

所以我正在尝试在网页上加载SelectField。当我使用浏览器连接localhost时,Flask崩溃。有了这条消息:“* Debugger PIN:320-071-095”

from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms import SelectField, RadioField

from main import mut_infos, year_infos

app = Flask(__name__)
app.config['SECRET_KEY'] = "BoomVroom"



class SelYear(FlaskForm):
    years = SelectField(u'year', choices= list(year_infos.keys()),coerce=int)

@app.route('/')
def index():
    form = SelYear()
    return render_template("index.html", form=form)

years_infos是一个以整数为键的字典。

这是index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Flask WebApp</title>
</head>
<body>
    <form method="POST" action="{{ url_for('index') }}">
        {{ form.csrf_token }}
        {{ form.years }}
    </form>

</body>
</html>

编辑:在终端上

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 320-071-095

在网页上,它只是说无法找到服务器“嗯。我们在查找该网站时遇到了麻烦。”

python flask flask-wtforms wtforms
1个回答
0
投票

我想你必须重新审视你的选择清单。可以在doc中找到,SelectField需要一个元组列表:qazxsw poi

choices = [('cpp','C ++'),('py','Python'),('text','Plain Text')]

你能做的是这样的:

https://wtforms.readthedocs.io/en/stable/fields.html

并将您的选择= list(year_infos.keys())指向该选择列表功能。

© www.soinside.com 2019 - 2024. All rights reserved.