在我的 Django 应用程序中,我写了一个带有forms.ChoiceField的表单 。选择应该是我的数据库中每几分钟更改一次的项目列表。当我重新加载页面时,我希望在下拉按钮中显示当前的项目列表。
我的代码运行良好,除了 forms.ChoiceField 不更新。要更新,我必须重新启动 Django 服务器。
我不知道我错过了什么,你能帮我吗?一定是小东西。
来自forms.py
class 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
杨__羊羊
相关分类