2012-01-10 44 views
0

我的形式是這樣的DJango中的表單無法正常工作。話說一個對象屬性不能是空,即使它不是

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") 


    def clean_story(self): 
     if len(self.cleaned_data['story']) < 4: 
      raise forms.ValidationError("Enter your full story") 

    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 = -48.543543543 
      lng1 = 34.543543543 
      # title1 = form.cleaned_data['title'] 
      titlepost = form.cleaned_data['title'] 
      ctxt = {'titlehere':titlepost} 
      catid = "test1234" 
      cat = Category(category=catid) 
      cat.full_clean() 
      cat.save() 
      marker = Marker(lat=lat1, lng=lng1,category=cat, title=titlepost) 
      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)) 

香港專業教育學院嘗試了所有我能仍然認爲,但我得到不同的錯誤信息。

有了這個代碼,我得到一個說

異常值:locations_marker.title不能爲null

的我應該做的任何建議。我只想在頁面上有一個表單,當它被提交時,在數據庫中創建一個新的對象。我一整天都在搞,無法搞清楚。

回答

3

您的clean_titleclean_story無論您是否更改了它,都需要返回乾淨的值。

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(self): 
     cleaned_data = self.cleaned_data 
     return cleaned_data 

下面是相關的Django文檔:https://docs.djangoproject.com/en/1.3/ref/forms/validation/#cleaning-a-specific-field-attribute

+0

感謝那些工作非常好先生。我現在遇到的問題是,當我嘗試使用十進制值驗證窗體時,它未被驗證。有任何想法嗎? – thebeagle 2012-01-10 19:51:53

+0

看起來你正在通過花車而不是小數。 – 2012-01-10 20:27:23