亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ValueError 無法將字符串轉換為浮點數:''

ValueError 無法將字符串轉換為浮點數:''

斯蒂芬大帝 2023-12-26 14:56:06
我在 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'


查看完整回答
反對 回復 2023-12-26
  • 1 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號