DJango的表格不起作用。假设对象属性不能为null,即使它不是

时间:2012-01-10 18:08:16

标签: python django 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")


    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

对我应该做什么的任何建议。我只想在页面上有一个表单,当它被提交时,在数据库中创建一个新对象。我整天都在搞乱,无法弄明白。

1 个答案:

答案 0 :(得分: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