這裏是我的表單,我試圖讓用戶傳遞兩個char字段和兩個座標,以便它們可以變成對象。我只是想出了char字段的驗證,但現在小數字段不會工作。有什麼建議麼?表單中的十進制值不會驗證Django
from django import forms
class SubmitForm(forms.Form):
title = forms.CharField(max_length=100)
story = forms.CharField(max_length=3000)
lat = forms.DecimalField(max_digits=25, decimal_places=20)
lng = forms.DecimalField(max_digits=25, decimal_places=20)
def clean_title(self):
if len(self.cleaned_data['title']) < 4:
raise forms.ValidationError("Enter your full title")
# Always return the cleaned data
return self.cleaned_data['title']
def clean_story(self):
if len(self.cleaned_data['story']) < 4:
raise forms.ValidationError("Enter your full story")
# Always return the cleaned data
return self.cleaned_data['story']
def clean_lng(self):
if self.cleaned_data['lng'] == 0.0:
raise forms.ValidationError("Enter your full story")
return self.cleaned_data['lng']
def clean_lat(self):
if self.cleaned_data['lat'] == 0.0:
raise forms.ValidationError("Enter your full story")
# Always return the cleaned data
return self.cleaned_data['lat']
def clean(self):
cleaned_data = self.cleaned_data
return cleaned_data
這是我的看法
def test(request):
ctxt = {}
if request.method == 'POST':
form = SubmitForm(request.POST) # A form bound to the POST data
if form.is_valid():
lat1 = form.cleaned_data['lat']
lng1 = form.cleaned_data['lng']
# title1 = form.cleaned_data['title']
titlepost = form.cleaned_data['title']
story1 = form.cleaned_data['story']
ctxt = {'titlehere':titlepost}
catid = "test1234"
cat = Category(category=catid)
cat.full_clean()
cat.save()
marker = Marker(lat=lat1, lng=lng1,category=cat, title=titlepost, story=story1)
marker.full_clean()
marker.save()
return render_to_response('home.html', ctxt, context_instance=RequestContext(request))
else:
return render_to_response('test.html', ctxt, context_instance=RequestContext(request))
else:
now = datetime.datetime.now()
form = SubmitForm()
latest_marks = Marker.objects.all().order_by('-submitted')[0:10]
ctxt = {
'marks':latest_marks,
'now':now.date(),
'form': form,
}
return render_to_response('test.html', ctxt, context_instance=RequestContext(request))
出於某種原因,每次我送的形式我得到這個錯誤信息
不是真的知道什麼回事。對Django來說還是很新的。任何幫助,將不勝感激。
編輯:添加冒號我失蹤了。錯誤依然存在
我修正了那個錯誤,表格仍然沒有驗證 – thebeagle 2012-01-10 20:48:30
您會得到同樣的確切錯誤嗎? – 2012-01-10 20:55:34
沒有錯誤消失,但窗體仍然不會驗證 – thebeagle 2012-01-10 20:57:20