Skip to content Skip to sidebar Skip to footer

ModelForm Data Not Saving Django

I've read the other posts on here about form data not saving, but can't seem to pinpoint my own problem. Have a feeling its something really basic I'm missing. Really appreciate t

Solution 1:

I am not able to comment here so i am writing here

1) HttpResponseRedirect('index.html') this is wrong here you have to pass some url like

/login/ or /profile/

2) please print your form like

print form and check what you are getting is all fields are valid or anything missing

and then tell the form error


Solution 2:

Try doing this:

forms.py

class AboutMeForm(ModelForm):
    class Meta:
        model=AboutMe
        fields = YOUR FIELDS

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(AboutMeForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(AboutMeForm, self).save(commit=False)
        if self.user:
            instance.user = self.user
        return instance.save()

on views.py:

def post_line(request):
    if request.method == 'POST':
        form = AboutMeForm(request.POST)
        if form.is_valid():
            new_about = form.save() 
            return HttpResponseRedirect('index.html')
    else:
        form = AboutMeForm()
    return render_to_response('post_line.html', locals(), context_instance=RequestContext(request))

Post a Comment for "ModelForm Data Not Saving Django"