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

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

Django:forms.ChoiceField 在新頁面加載后不會刷新

Django:forms.ChoiceField 在新頁面加載后不會刷新

精慕HU 2021-07-19 17:05:51
在我的 Django 應用程序中,我寫了一個帶有forms.ChoiceField的表單 。選擇應該是我的數據庫中每幾分鐘更改一次的項目列表。當我重新加載頁面時,我希望在下拉按鈕中顯示當前的項目列表。我的代碼運行良好,除了 forms.ChoiceField 不更新。要更新,我必須重新啟動 Django 服務器。我不知道我錯過了什么,你能幫我嗎?一定是小東西。來自forms.pyclass BookingForm(forms.ModelForm):    make_list_of_tuple = lambda list_machine  :  [tuple( [i,i]) for i in list_machine]    MACHINES= tuple( QA_machine_DB.objects.values_list('QAmachine',flat=True))    CHOICE_QA_MACHINES= make_list_of_tuple(MACHINES)    QAmachine= forms.ChoiceField(choices=CHOICE_QA_MACHINES)    class Meta():        model= QA_machine_DB        fields= ['QAmachine', 'owner', 'comments','status']        # http://nanvel.name/2014/03/django-change-modelform-widget        widgets = {                   'owner':forms.TextInput(attrs={'placeholder':'owner'}),                   'comments':forms.TextInput(attrs={'placeholder':'comments'}),                   'status': forms.RadioSelect( choices=[('busy','busy'),('free','free')])}從模板<form   class="form-group" method="post" novalidate >    {% csrf_token %}     <table >       <td>          {{ BookingForm.QAmachine}}        </td>         <td>           {{ BookingForm.owner.errors }}           {{ BookingForm.owner}}         </td>         <td>           {{ BookingForm.comments.errors }}           {{ BookingForm.comments}}         </td>         <td>           {% for radio in BookingForm.status %}           {{ radio }}           {% endfor %}         </td>    </table>  <input type="submit" class="btn btn-success" value="submit status change" style="float: right;"     > 坦克你在Advance
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

不,在類級別定義的任何內容只會在第一次導入類時評估一次。

您可以在__init__方法中執行此操作,但更好的方法是使用用于從查詢集中獲取選擇的字段:ModelChoiceField。


查看完整回答
反對 回復 2021-07-21
  • 1 回答
  • 0 關注
  • 347 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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