形式中的十进制值不会验证Django

时间:2012-01-10 20:34:11

标签: python django django-forms django-views

这是我的表单我试图用户传递两个char字段和两个坐标,以便它们可以转换为对象。我只是想出了char字段的验证,但现在小数字段不起作用。有什么建议吗?

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 个答案:

答案 0 :(得分:3)

你在表单的干净方法中使用if语句后遗漏了一些冒号。

答案 1 :(得分:1)

您考虑使用Modelforms吗?

您可以通过使用Marker模型的模型来简化代码(减少代码重复,通常用于创建模型的代码更少......)

供参考: 您可以将min_length发送到CharField并免费获得len()验证:)

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

答案 2 :(得分:0)

  

出于某种原因,每次我发送表单时都会收到此错误消息

我看不出错误! 但是我尝试了这个并且它运作正常。

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']