我有一個非常簡單的形式布爾字段返回Django中的真實的形式
class IcraveSatisfiedForm(forms.Form):
satisfied_or_not = forms.BooleanField(label="Was your Crave satisfied?")
satisfier = forms.CharField(label="Satisfier")
def clean_satisfier(self):
satisfier = self.cleaned_data['satisfier']
satisfied = self.cleaned_data['satisfied_or_not']
if satisfied == True:
if satisfier !='':
try:
user = User.objects.get(username__iexact = satisfier)
except:
user = None
if not user :
raise forms.ValidationError('The username is not valid')
else:
return user
else:
return None
我要清理這種形式和檢驗在正在used.But出於某種原因 self.cleaned_data [」值satisfied_or_not']總是返回True。我究竟做錯了什麼 ?
編輯:我在觀以及
def my_craves(request,crave_id = None):
craves = Icrave.objects.filter(person = request.user,satisfied_or_not = False)
if request.method == 'POST':
if 'satisfied_or_not' in request.POST:
my_craves_form =IcraveSatisfiedForm(request.POST,user = request.user)
if my_craves_form.is_valid() :
satisfied = my_craves_form.cleaned_data['satisfied_or_not']
satisfier = my_craves_form.cleaned_data['satisfier']
試過這種
Request.POST
<QueryDict: {u'csrfmiddlewaretoken': [u'21bdde0fdac2b18101e1c42ba2f92843'], u'satisfier': [u'admin'], u'satisfied_or_not': [u'Update']}>
我使用正常的形式它仍然返回爲真渲染數據
my_craves_form = IcraveSatisfiedForm()
return render(request, 'icrave/view/my_craves.html', {'craves' : craves,'my_craves_form':my_craves_form})
你真的有兩個名稱相同的字段? – second
沒有我的壞...複製錯誤的字段 – Akamad007
你能顯示request.POST的值嗎? –