이 내보기에서 선택을 무시 곳이 내 forms.py장고 다중 선택, 선택을 재정의하는 방법을 제대로
CHOICES = []
class salDeptChartForm(forms.Form):
company = forms.CharField(max_length=2,label = 'Firma',help_text='A valid email address, please.')
date_validfrom = forms.DateField(label = 'Bu Tarihten',required=False)
date_validuntil = forms.DateField(label = 'Bu Tarihe Kadar',required=False)
saldept = forms.MultipleChoiceField(label = 'Satış Departmanları',choices=CHOICES, widget=forms.CheckboxSelectMultiple())
입니다.
form = salDeptChartForm(initial={'company':'01'})
saldeptlist = saleinstance.fetchSalDept()
form.fields['saldept'].choices = saldeptlist <this is where I override>
옵션 중 하나를 선택하면 문제가 발생합니다. 양식은 검증을하지 않습니다.
Select a valid choice. * is not one of the available choices.
내보기에서 선택 사항을 무시한다고해도 장고는 내가 만든 이전 선택 사항을 계속 확인합니다. 나는 정확한 html 출력을 어렵게 만든다.
어떻게 극복 할 수 있습니까? thx
완전한보기 코드가 있습니다. 양식은 get과 post에 대해 두 번씩 하나씩 시작합니다. 당신은 단지 GET
방법의 선택을 덮어 쓰기 때문에
def salDept(request):
member_id = request.session['member_id']
saleinstance = sale(member_id)
chartinstance = charts(member_id)
if request.method == 'GET':
form = salDeptChartForm(initial={'company':'01'}) <first init>
saldeptlist = saleinstance.fetchSalDept() <its a list>
form.fields['saldept'].choices = saldeptlist <override choices>
print 'get worked'
return render(request, 'chart/sale/salDept.html',locals())
if request.method == 'POST':
form = salDeptChartForm(request.POST) <second init>
print 'post worked'
if form.is_valid(): <fails>
print 'valid'
company = form.cleaned_data['company']
vfr = form.cleaned_data['date_validfrom']
vun = form.cleaned_data['date_validuntil']
validfrom = formatDate(vfr)
validuntil = formatDate(vun)
selectedSalDepts = request.POST.getlist('saldept')
else:
print 'not valid'
print form.errors
resultdict = chartinstance.salesBySaldept(company,selectedSalDepts,validfrom, validuntil)
form = salDeptChartForm(initial={'company':company,'date_validfrom':request.POST['date_validfrom'], 'date_validuntil':request.POST['date_validuntil']})
domcache = 'true'
return render(request, 'chart/sale/salDept.html',locals())
코드 반복을 좋아하지 않으므로 한 가지 양식 초기화 및 하나의 선택 재정의로이 작업을 수행하는 방법이 있습니까? – durdenk
예. 일부 도우미 함수를 작성해야하거나 클래스 기반보기를 조사 할 수도 있습니다. 그들은 이런 상황에서 완벽 할 것입니다. 이것은 그들에게 좋은 소개입니다 : http://www.youtube.com/watch?v=yr9HOLipPXc – miki725