2012-01-10 45 views
0

這裏是我的表單,我試圖讓用戶傳遞兩個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來說還是很新的。任何幫助,將不勝感激。

編輯:添加冒號我失蹤了。錯誤依然存在

回答

3

您在表單的乾淨方法中的if語句之後缺少一些冒號。

+0

我修正了那個錯誤,表格仍然沒有驗證 – thebeagle 2012-01-10 20:48:30

+0

您會得到同樣的確切錯誤嗎? – 2012-01-10 20:55:34

+0

沒有錯誤消失,但窗體仍然不會驗證 – thebeagle 2012-01-10 20:57:20

1

您是否考慮過使用Modelforms

您只需使用的ModelForm爲標記模型簡化代碼(更少的代碼複製,在一般情況更少的代碼來創建模型...)

FYI: 您可以將MIN_LENGTH發送到CharField,並得到免費:)

class SubmitForm(forms.Form): 
    title = forms.CharField(max_length=100, min_length=4) 
0

出於某種原因,每次發送的表格時,我得到這個錯誤信息LEN()驗證

我看不到錯誤! 但我試過這個,它工作正常。

import math 
class MyForm(forms.Form): 
    title = forms.CharField(max_length=100, min_length=4) 
    story = forms.CharField(max_length=3000, min_length=4) 
    lat = forms.DecimalField(max_digits=25, decimal_places=20) 
    lng = forms.DecimalField(max_digits=25, decimal_places=20) 

    def clean_lat(self): 
     lat = self.cleaned_data['lat'] 
     if math.ceil(float(lat)) <= 0: 
      raise forms.ValidationError("Enter your full story") 
     return self.cleaned_data['lat'] 

    def clean_lng(self): 
     lng = self.cleaned_data['lng'] 
     if math.ceil(float(lng)) <= 0: 
      raise forms.ValidationError("Enter your full story") 
     return self.cleaned_data['lng'] 
+0

仍然沒有嘗試的運氣。謝謝你嘗試。 – thebeagle 2012-01-11 02:38:52