在django应用程序的表单中使用ChoiceField时, 选项和“选择”标记显示在源代码(检查器)中,而不显示在页面中。
使用CharField时不会发生此问题。
我从模型中得到选择,但是即使手动设置选择,问题仍然存在。
这里是Forms.py
:
from django import forms
from .models import Placard, Agent
class FormDossier(forms.Form):
j = 0
placard_options = []
agent_options = []
for i in Placard.objects.all().order_by('rang'):
tup = str(j), i.rang
placard_options.append(tup)
j=j+1
for i in Agent.objects.all().order_by('numero_de_somme'):
tup = str(j), i.numero_de_somme
agent_options.append(tup)
j=j+1
f_agent = forms.ChoiceField(widget=forms.Select, choices=agent_options,label = "رقم التأجير" )
f_placard = forms.ChoiceField(widget=forms.Select, choices=placard_options,label = "الخزانة")
f_tiroir = forms.CharField(label = "الرف")
View.py
def CreateDossier(request):
form = FormDossier(request.POST or None)
if form.is_valid():
_p = form.cleaned_data['f_placard']
_a = form.cleaned_data['f_agent']
t = form.cleaned_data['f_tiroir']
p = Placard.object.get(rang=_p)
a = Agent.object.get(numero_de_somme=_a)
Dossier(agent=a, placard=p, tiroir=t)
return HttpResponseRedirect('/')
return render(request, 'dossier/create.html', locals())
模板
<div class="row">
<form method="post" class="col s12">
<div class="row">
{% csrf_token %}
{{ form.as_p }}
</div>
</form>
</div>