首先,我應該說我是一個新手web2Py用戶。我遇到了一個看似簡單的問題,但我所嘗試的一切似乎都失敗了。我想單擊一個圖像,當它選中它時,它會在數據庫中給它一個True值,當我「解除」它時,它會給出一個False值。Web2Py在點擊圖片後將值附加到數據庫
我使用的web2py作爲一個框架,我有以下數據庫:
db.define_table(
'interestFood',
Field('Tacos', 'boolean', default=False),
Field('Salad', 'boolean', default=False),
Field('Cheeseburger', 'boolean', default=False),
Field('Pizza', 'boolean', default=False),
Field('Sushi', 'boolean', default=False),
Field('Breakfast', 'boolean', default=False))
db.interestFood.id.writable = db.interestFood.id.readable =假
而且我有下列的控制器:
def createProfile_interests():
if (db(db.interestFood.id==auth.user.id).count()!=1):
db.interestFood.insert(id=auth.user.id)
interest1=db(db.interestFood.id==auth.user.id).select()
print(interest1)
return dict(interest1=interest1)
這裏是代碼的HTML部分:
{{Tacos=False
Salad=False
Sushi=False
Cheeseburger=False
Pizza=False
Breakfast=False}}
<input type="image" name = "food" id="0" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/taco.png" onclick=interests(0) onclick="{{interest1.update(Tacos=True)}}">
<input type="image" name = "food" id="1" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/pizza.png" onclick=interests(1) onclick="{{interest1.update(Pizza=True)}}">
<input type="image" name = "food" id="2" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/salad.png" onclick=interests(2) onclick="{{interest1.update(Salad=True)}}"><br><br>
<input type="image" name = "food" id="3" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/burger.png" onclick=interests(3) onclick="{{interest1.update(Cheeseburger=True)}}">
<input type="image" name = "food" id="4" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/sushi.png" onclick=interests(4) onclick="{{interest1.update(Sushi=True)}}">
<input type="image" name = "food" id="5" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/breakfast.png" onclick=interests(5) onclick="{{interest1.update(Breakfast=True)}}">
interest()函數是一個只允許用戶選擇兩個圖像的約束。
我試圖做這樣的事情:
<input type="image" name = "food" id="0" src="http://127.0.0.1:8000/WagMore/static/_2.14.6/images/taco.png" onclick=interests(0) onclick="
{{
if Tacos==False:
interest1.update(Tacos=True)
else:
interest1.update(Tacos=False)
}}">
但是,這是行不通的。我一直在爲此工作了大約兩天。任何想法如何去解決這個問題?我很感激!