我在 heroku 上安裝了我的應用程序。我使用timeme js記錄用戶在頁面上花費的活躍時間,并使用隱藏表單將值存儲到數據庫中。此外,我正在使用 otree 包來編寫我的應用程序。以下是我使用的代碼:models.pyclass Active(ExtraModel): active_duration = models.FloatField() active_record = models.LongStringField()otree 中的views.py/pages.pyclass intro_instructions(Page): def before_next_page(self): data = self.form.data p = self.player active = Active.objects.create(active_duration=data['active_duration'], active_record=data['active_record']) active.save()html<form method="post" role="form" id="form" autocomplete="off">{% csrf_token %} <input type="text" name="active_record" id="id_active_record" style="display: none" > <input type="number" step='any' name="active_duration" id="id_active_duration" style="display: none"></form>錯誤ValueErrorcould not convert string to float: ''{ "csrfmiddlewaretoken": "vVhVRLrAUokmiNGRpzCaP78bnTOQyowf5VMfIDgKGglWGuEyQAU2wooWjQzXuBgD", "active_duration": "", "active_record": ""}是因為 active_duration 為空嗎?如果我為表單設置 Blank=true, null=true 會有幫助嗎?我假設每次使用都會有輸入價值。還有關于為什么輸入為空的任何想法嗎?難道是用戶使用腳本跳過了沒有可見字段的頁面?從哨兵錯誤消息來看,一名用戶發生了兩次這種情況。
1 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
這是因為你不能將空字符串轉換為浮點數。不過你可以這樣做:
active_duration=data.get('active_duration') or "0"
如果 data["active_duration"] 為空、False 或 None,這將為您提供“0”。
>>> data = {"foo": ""}
>>> data.get("foo") or "bar"
'bar'
添加回答
舉報
0/150
提交
取消