tangowithcode / tango_with_django_19

A beginners guide to web programming with Python / Django 1.9
Apache License 2.0
207 stars 188 forks source link

Chapter 7 def add_category gives exceeption when a POST is received with form validation errors #17

Closed martindegroot closed 8 years ago

martindegroot commented 8 years ago

Hello Leif and David,

Current structure of def add_category: if request.method == 'POST' ... if forrm,is_valid():

else: 
    form contains errors, they are printed to the console and then execution of the code stops
   without a return statement, meaning that None will be returned

else: request was not a post, form is set to CategoryForm() and a correct return statement is given

martindegroot commented 8 years ago

Again I seem to have submitted the issue before I was ready. As the code stands now, the first time through this code, is correctly handled, displaying an empty category form The case of a correctly filled form submitted by the user is also correctly handled, resulting in a new category saved in the database. It is the case of an incorrectly filled form which is now wrongly handled. Instead of the form being displayed with the form errors, an exception is raised because the function does not return a HTTPResponse but it returns None. Possible solutions: 1) copy the final line return render(request, 'rango/add_category.html', {'form': form }) to the end of the else block for an invalid form

2) Remove the else branch from the if request.method == 'POST' and instead make that code at the same indentation as the if statement (if request.method == 'POST' ) Move the form assignment form = CategoryForm() to the top of the code The final code then contains only the return statement and this code will be executed for an invalid form containing errors and for the case that the request.method was not a POST i.e. for a new form

I prefer the second solution, and it seems from the comments just beforee the final return that you also intended that this code should handle the bad form and the new form

I will try to attach my copy of rango.views.py to this comment. After copying the def add_category code to a .txt file it was successfully attached to this comment.

I guess you didn't test this code for the case of an invalid form (submitting the form with an empty string for the name field)? I am still enjoying learning Django with the help of your book. Martin de Groot

views_py.txt

leifos commented 8 years ago

Yeah, i think the indentation got a little messed up, but looking at the code and hearing your suggestions, I think that making the form assignment earlier is a bit cleaner.

maxwelld90 commented 8 years ago

Thank you, Martin! Hmm - I am not sure if this has been tidied up now, @leifos?

Going through what I see in Chapter 7 now...

Make a blank form

If POST, the recreate the form from the POST data. If the form is valid, save and exit out the function. If invalid, print the errors to stdout.

Then we drop back out the two conditionals, meaning that the final return render() should cover both a GET request (blank), or an invalid form from a POST.

I guess it's been fixed up, as it now looks like Martin's suggestion...

leifos commented 8 years ago

Hi David, hi Martin,

Yeah, I remember going through this again and changing it about.. So hopefully it is all good now.

Thanks L

Dr. Leif Azzopardi Chancellor's Fellow / Senior Lecturer Dept. of Computer & Information Sciences University of Strathclyde Glasgow

Ph: 0141 548 3617 Email: leif.azzopardi@strath.ac.uk Alt Email: leifos@acm.org Skype: leifos Twitter: leifos GitHub: leifos


From: David Maxwell [notifications@github.com] Sent: Tuesday, September 27, 2016 12:52 PM To: leifos/tango_with_django_19 Cc: Leif Azzopardi; Mention Subject: Re: [leifos/tango_with_django_19] Chapter 7 def add_category gives exceeption when a POST is received with form validation errors (#17)

Thank you, Martin! Hmm - I am not sure if this has been tidied up now, @leifoshttps://github.com/leifos?

Going through what I see in Chapter 7 now...

Make a blank form

If POST, the recreate the form from the POST data. If the form is valid, save and exit out the function. If invalid, print the errors to stdout.

Then we drop back out the two conditionals, meaning that the final return render() should cover both a GET request (blank), or an invalid form from a POST.

I guess it's been fixed up, as it now looks like Martin's suggestion...

� You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/leifos/tango_with_django_19/issues/17#issuecomment-249843355, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AB02B_50a6b7305o0Qq0oLY9dENwjZ8Tks5quQOSgaJpZM4IcSID.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/leifos/tango_with_django_19","title":"leifos/tango_with_django_19","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/leifos/tango_with_django_19"}},"updates":{"snippets":[{"icon":"PERSON","message":"@maxwelld90 in #17: Thank you, Martin!\r\nHmm - I am not sure if this has been tidied up now, @leifos?\r\n\r\nGoing through what I see in Chapter 7 now...\r\n\r\nMake a blank form\r\n\r\nIf POST, the recreate the form from the POST data.\r\nIf the form is valid, save and exit out the function.\r\nIf invalid, print the errors to stdout.\r\n\r\nThen we drop back out the two conditionals, meaning that the final return render() should cover both a GET request (blank), or an invalid form from a POST.\r\n\r\nI guess it's been fixed up, as it now looks like Martin's suggestion..."}],"action":{"name":"View Issue","url":"https://github.com/leifos/tango_with_django_19/issues/17#issuecomment-249843355"}}}