thebeagle
thebeagle

Reputation: 338

Forms in DJango not working. Saying an objects attribute cant be null even though it isn't

My form is like this

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

The view is like this

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

Ive tried all that I can think of but i still get different error messages.

With this code I get one that says

Exception Value: locations_marker.title may not be NULL

Any suggestions of what I should do. I just want to have a form on a page that when it is submitted a new object is created in the database. I've been messing around the entire day and cant figure it out.

Upvotes: 0

Views: 142

Answers (1)

Mark Lavin
Mark Lavin

Reputation: 25164

Your clean_title and clean_story need to return the clean value regardless of whether you've changed it or not.

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

Here are the relevant Django docs: https://docs.djangoproject.com/en/1.3/ref/forms/validation/#cleaning-a-specific-field-attribute

Upvotes: 3

Related Questions