Problems with saving data in form-model Django

Asked

Viewed 1,011 times

4

Gentlemen, good afternoon to you! I’m facing a small problem, I made some changes in an application I’m developing for studies. I come across the following, fill the form all right and when I go to save, nothing happens, does not redirect to the listing or anything. The stranger I look at in a post terminal. "POST /tickets/new/ HTTP/1.1" 200 3261 What could it be? I’ve tried everything and I can’t fix it.

Follow the view code : http://hastebin.com/uxorenoyiy.py Follow the model code: http://hastebin.com/uxogiqufoh.py Who could help me Graduate.

  • You are running via runserver or is on a "real" webserver (e.g.: Apache)? Is there anything in the console/log file? The flag DEBUG is True? By the way, I’m assuming /tickets/novo/ map to ticket_create, or I’m wrong?

2 answers

2

Note that the status of the reply was 200 which means that there was some mistake form validation and that most likely a render() was executed at the end of the request.

If there was no validation error in the form, the answer would be a redirect, status-oriented 302.

Therefore, I believe that the POST is failing validation and the same page is being re-xibided by the browser. As suggested by @mgibsonbr.

  • Good observation! Indeed, most of POSTs redirects to a GET after succeeding - so that the browser "Back" button does not accidentally repeat the first POST. If you didn’t redirect, you failed...

  • 1

    Correct. This practice is the most recommended and known PRG (Post-redirect-GET).

1

I have no experience with Forms, but most likely your problem is that there are no indications of errors in your template. This is your line on ticket_post:

if not form.is_valid():
        return render(request, 'ticket_novo.html',{'form': form})

He’s saying: "if the validation fails, show the same page I’m already on, with the same form, with the same values I passed". That is, it is quite possible that the action has changed page, and you didn’t even notice, because the new page is identical to the old (and page transitions not always accompanied by a visual indication that something has occurred).

However, as per that answer in Soen if your template somehow uses the forms.errors then the new tab can visually indicate what went wrong. You say " fill the form all right" but do not show nor Form nor the data you tried to pass, so the possibility of error when filling in the form (i.e. invalid data) should be considered, no?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.