Skip to content Skip to sidebar Skip to footer

Django Form Request Not Saving Data To Db

I am using a django form in atemplate to save data entered to database. In my view after the request is made, the response is redirected correctly but the data is not saved to db.

Solution 1:

In python, indentation matters.

def testview(request):
    if request.method== 'POST':
        form=MapForm(request.POST)
        if form.is_valid():
            test1=request.POST.get('t1')
            print meaningid1
            test2=request.POST.get('t2')
            print meaningid2
            pobj=testmodel(test1=test1,test2=test2)
            pobj.save()
        return HttpResponse('Successful')

In the above code 'Successful' will be displayed regardless of whether the form is actually successful or not. You need to push your return statement four spaces to the right, and you also need to add an else clause which handles the situation where the form is not valid. Typically that is just to display the form again (with form errors which wil be displayed for you automatically is you use form.as_p or form.as_table)

Post a Comment for "Django Form Request Not Saving Data To Db"