因此,我一直在创建一个可以在在线购物站点中收藏商品的用户数据库。即使当我输入b.title和board时,它们似乎都打印“ u'board name'”,我似乎还是收到了此错误。 应该这样,以便当某人选中“董事会”复选框时,该服装与董事会的关系应添加到数据库中。
<form action="" method="POST" name="boards_list">
<table class="table is-striped">
{% for board in boards %}
<tr>
<td>
<label class="checkbox">
<input type="checkbox" name="board_titles" value="{{board.title}}"/>
</label>
<a href="#">{{board.title}}</a>
</td>
</tr>
{% endfor %}
</table>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="button is-small">Add To Favs</button>
</div>
</div>
</form>
这是上面显示的Jinja2模板中的代码。以下是源代码:
if request.method =="POST":
f_b_titles = request.form.getlist("board_titles")
clothing = Clothing.query.filter_by(name = name).first()
userid = int(current_user.id)
clothingid = int(clothing.id)
for b in boards:
for board in f_b_titles:
if b.title == board:
fav_relationship = Favourites_relationship(user_id = userid, clothing_id = clothingid, favs_board = b.title)
db.session.add(fav_relationship)
db.session.commit()
flash('Added favourite to mood board!', 'success')
最后,来自数据库模型的代码:
class Favourites_relationship(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), unique=True, nullable=False)
clothing_id = db.Column(db.Integer, db.ForeignKey('clothing.id'),nullable=False)
board_title = db.Column(db.Unicode, db.ForeignKey('favourites_board.title'), nullable=False)
和:
class Favourites_board(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
favourites_relationships = db.relationship('Favourites_relationship', backref='favs_board', lazy=True)
有关错误的更多信息:
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/leawhitelaw/Desktop/online_shop_data/flaskshop/routes.py", line 97, in product_details
fav_relationship = Favourites_relationship(user_id = userid, clothing_id = clothingid, favs_board = b.title)
答案 0 :(得分:0)
尝试
fav_relationship = Favourites_relationship(user_id = userid, clothing_id = clothingid, favs_board = b)
基本上,该错误表示您只能将Favourites_board
个实例(例如fb = Favourites_board()
)分配给某个关系。
因此,假设木板是Favourites_board
实例的列表,您只需将清单中的木板(b
)分配给favs_board=...
。
答案 1 :(得分:0)
board_title = db.Column(db.Unicode, db.ForeignKey('favourites_board.title'), nullable=False)
这行是错误的
来自here
使用Unicode类型时,仅适合传递Python unicode对象,而不是纯str。如果在下面传递了一个普通的str Python 2发出警告。如果您注意到自己的申请 发出这些警告,但您不确定它们的来源, Python警告过滤器,记录在 http://docs.python.org/library/warnings.html,可用于转向 这些警告变成异常,将说明堆栈跟踪:
由于您要从表单传递原始值,因此需要python对象。
尝试使用:
db。字符串
db.Text
或db.Unicodetext