Python “从自定义Wigit返回值”复选框
我正在编写一段代码,要求用户从复选框中选择浇头,他们可以选择多个浇头。当他们按“提交”时,我想返回从数据库查询的浇头列表。 当我运行程序时,我能够获得多个复选框,但是当我按下“提交浇头”按钮时,我收到以下错误:Python “从自定义Wigit返回值”复选框,python,flask,sqlalchemy,Python,Flask,Sqlalchemy,我正在编写一段代码,要求用户从复选框中选择浇头,他们可以选择多个浇头。当他们按“提交”时,我想返回从数据库查询的浇头列表。 当我运行程序时,我能够获得多个复选框,但是当我按下“提交浇头”按钮时,我收到以下错误: sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 0 - probably unsupported type. [SQL: INSERT INTO pizza_topppings
sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 0 - probably unsupported type.
[SQL: INSERT INTO pizza_topppings (toppings) VALUES (?)]
[parameters: (['Pepperoni', 'Extra', 'Cheese'],)]
(Background on this error at: http://sqlalche.me/e/13/rvf5)
我如何解决这个问题,以便程序返回选定的浇头
这是密码
app.py
import os
from flask_wtf import FlaskForm
from flask import Flask, render_template, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from forms import ToppingForm
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecretkey'
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app,db)
class PizzaTopppings(db.Model):
id = db.Column(db.Integer, primary_key=True)
toppings = db.Column(db.Text)
def __init__(self, toppings):
self.toppings = toppings
def __repr__(self):
return f"Toppings requested: {self.toppings}"
@app.route('/')
def index():
return render_template('index.html')
@app.route('/addtopping', methods=['GET', 'POST'])
def add_toppings():
form = ToppingForm()
if form.validate_on_submit():
toppings = form.toppings.data
new_toppings = PizzaTopppings(toppings)
db.session.add(new_toppings)
db.session.commit()
return redirect(url_for('list_toppings'))
return render_template('add_toppings.html', form = form)
@app.route('/listoftoppings')
def list_toppings():
pizza_toppings = PizzaTopppings.query.all()
return render_template('toppinglist.html', pizza_toppings= pizza_toppings)
if __name__ == '__main__':
app.run(debug=True)
forms.py
from flask_wtf import FlaskForm
from wtforms import SelectMultipleField, SubmitField, widgets
class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
class ToppingForm(FlaskForm):
string_of_files = ['Pepperoni\r\nExtra Cheese\r\nGreen Peppers\r\nSpinach\r\nPineapple']
list_of_files = string_of_files[0].split()
# create a list of value/description tuples
files = [(x, x) for x in list_of_files]
toppings = MultiCheckboxField('toppings', choices=files)
submit = SubmitField('Submit toppings')
添加_toppings.html
{% extends 'base.html' %}
{% block content %}
<div class="jumbotron">
<h1>Customer Information</h1>
<p>Provide your information to begin your order.</p>
<form method="POST">
{{form.hidden_tag()}}
{{form.toppings.label}} {{form.toppings()}}
{{form.submit()}}
</form>
</div>
{% endblock %}
{%extends'base.html%}
{%block content%}
客户信息
提供您的信息以开始订购
{{form.hidden_tag()}}
{{form.toppings.label}{{form.toppings()}}
{{form.submit()}}
{%endblock%}
toppinglisy.html
{% extends 'base.html' %}
{% block content %}
<div class= 'jumbotron'>
<h1>Order Toppings Here</h1>
<ul>
{% for t in pizza_toppings %}
<li>{{t}}</li>
{% endfor %}
</ul>
</div>
{% endblock content %}
{%extends'base.html%}
{%block content%}
在这里点浇头
{比萨中的t为%u配料%}
- {{t}
{%endfor%}
{%endblock内容%}
谢谢你的帮助