我不知道我在做错什么,因为与其他示例一样,但是我正在尝试使用一种形式将新的航班添加到sql数据库并将其显示在html页面上,现在当我尝试发布形式,我收到此错误ValueError:词典更新序列元素#0的长度为1;需要2个
这是表单类:
class NewFlight(FlaskForm):
flight_number = IntegerField('Flight Number', validators=[DataRequired()])
date = StringField('Departure date', validators=[DataRequired()])
picture = FileField('Upload Cover Image', validators=[FileAllowed(['jpg', 'png'])])
destination = StringField('Destination Airport', validators=[DataRequired()])
fly_from = StringField('Departure Location', validators=[DataRequired()])
price = IntegerField('Price', validators=[DataRequired()])
submit = SubmitField('Add Flight')
和用于SQL的模型类:
class CurrentFlights(db.Model):
id = db.Column(db.Integer, primary_key=True)
flight_number = db.Column(db.Integer, unique=True, nullable=False)
date = db.Column(db.String(100), nullable=False)
image = db.Column(db.String(20), nullable=False, default='plane.jpg')
destination = db.Column(db.String(100), nullable=False)
fly_from = db.Column(db.String(100), nullable=False)
price = db.Column(db.Integer, nullable=False)
和路线部分:
@app.route('/add_flight', methods=['GET', 'POST'])
def add_flight():
form = NewFlight()
if form.validate_on_submit():
flight = NewFlight(flight_number=form.flight_number.data, data=form.date.data, image=form.picture.data, destination=form.destination.data, fly_from=form.fly_from.data, price=form.price.data)
db.session.add(flight)
db.session.commit()
flash('New Flight added to database!', 'success')
return redirect(url_for('home'))
return render_template('admin_AddFlights.html', title='Add New Flight', form=form)
这可能是一个愚蠢的错误,但是可以得到任何帮助,
谢谢!
回溯:
C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask_sqlalchemy\__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [06/Feb/2019 22:49:12] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [06/Feb/2019 22:49:15] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [06/Feb/2019 22:49:16] "GET /admin_menu HTTP/1.1" 200 -
127.0.0.1 - - [06/Feb/2019 22:49:20] "GET /admin_menu HTTP/1.1" 200 -
127.0.0.1 - - [06/Feb/2019 22:49:38] "POST /admin_menu HTTP/1.1" 302 -
127.0.0.1 - - [06/Feb/2019 22:49:38] "GET /admin_home HTTP/1.1" 200 -
127.0.0.1 - - [06/Feb/2019 22:49:58] "GET /add_flight HTTP/1.1" 200 -
[2019-02-06 22:50:15,100] ERROR in app: Exception on /add_flight [POST]
Traceback (most recent call last):
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\flight\routes.py", line 118, in add_flight
flight = NewFlight(flight_number=form.flight_number.data, data=form.date.data, image=form.picture.data, destination=form.destination.data, fly_from=form.fly_from.data, price=form.price.data)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\wtforms\form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\flask_wtf\form.py", line 88, in __init__
super(FlaskForm, self).__init__(formdata=formdata, **kwargs)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\wtforms\form.py", line 278, in __init__
self.process(formdata, obj, data=data, **kwargs)
File "C:\Users\CarlSecondAccount\PycharmProjects\flight_tracker\pythonmain\lib\site-packages\wtforms\form.py", line 124, in process
kwargs = dict(data, **kwargs)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
127.0.0.1 - - [06/Feb/2019 22:50:15] "POST /add_flight HTTP/1.1" 500
答案 0 :(得分:0)
从事物的外观来看,您需要更改此行
flight = NewFlight(
flight_number=form.flight_number.data,
data=form.date.data,
image=form.picture.data,
destination=form.destination.data,
fly_from=form.fly_from.data,
price=form.price.data)
对此
flight = CurrentFlights(
flight_number=form.flight_number.data,
data=form.date.data,
image=form.picture.data,
destination=form.destination.data,
fly_from=form.fly_from.data,
price=form.price.data)
因为NewFlight
用于初始化表单,而CurrentFlights
用于初始化数据库模型