Form is invalid

Submitted 3 years, 10 months ago
Ticket #18
Views 251
Language/Framework Django
Priority High
Status Closed

i am trying to update a record from mysql database. Following is my update func.

def update(request,id):
    user=Login.objects.get(id=id)
    form=LoginForm(request.POST, instance=user)
    if form.is_valid():
        print("Form is valid.")
        form.save()
        return redirect("/show")
    else:
        print("Form is In-valid.")
    return render(request,"edit.html",{"qs":user})

in the terminal it is saying that form is In-valid as i printed in the command of else part so this means the form that i created above is not valid dont know what to do kindly help.

Submitted on Jun 08, 20
add a comment

1 Answer

Acknowledged

Submitted 3 years, 10 months ago

dont pass the user instance to the form creation, form=LoginForm(request.POST or None). Try this

- scott 3 years, 10 months ago

By doing this wont be the values of the fields that needed to be save would be lost ?

- moazamrana22 3 years, 10 months ago

after form valid add the user . It should work. like form = form(commit=false form.user = user form.save(). try something like this

- Vengat 3 years, 10 months ago

As we are not getting any update on this ticket, as you raised as HIGH priority. Hopefully you have resolved the issue. So we go ahead and close the ticket. In case if doesnt work , then submit the new one.

- Vengat 3 years, 10 months ago


Latest Blogs