Wednesday, October 1, 2014

Re: Newbie Question: Django Tutorial Part 4 - Exception Issue

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJULF7YAAoJEB267zEX1rwQdRAP/3eHv6DPSL9JhzHc8LpJDoEv
qMwsf6A/YtnJeH5tt0pWGDHiD3PjvU7dz0Cwtf8b+Q8kMg97mzoWWfoDhLNGblk3
TdgH461MSSOCwFOnaCAKCpP/btYcEeuWk1nP6ykwQaxWJntoF7SFPU8XaTuf5gib
5vJqTVcOs0A6/jCbui1+5BkNv4OSgiNdjW8pjK2nAipAus4XebS4f51gKFpLBAns
CLp9zJ+i7uU0n4IKH69lOsOLgrkHvcFWANx9uDZZojRYd8lWUeBhmWlRzJQqysZZ
ne5VLeZTZwVNLgg01ekttwjKBa6d7fTS6YaDIOpPx/X07ivp5bhMBACa7GODeVG6
0KrhtakeTzg1VqIjPwjNW58NrEmb0obpIcvCGREenuxl+PwlKDW2BwnrBHP90Dql
x7vot9nBDX1Z+ddFXih09OD8w0eilDM0prlehgep/VQpFNu+5kVDnPvsUb3Ne/JF
M8BoPiRKmZCnss9+WJg8JuqZYXZ5Sb63OiE08xOUXhhl5naeggOXUpjljBIAuwxX
pci91MVRb6iFHNO5wgLkKAmeQFo8MBsfIuSDQ2v6CHFmBa4EeyykOK27uPIAI5Ef
S9yFc0/VnZovynB43V+7GFga2irlvOMUs0eTa1rRKWiSlgEwqncNggPqbZ5oE7xB
Wvgdupxmkoaeq21M2TX3
=ft5O
-----END PGP SIGNATURE-----
My pleasure ;)
Enjoy coding!


On 01 Oct 2014, at 22:04, zaiks0105 <lahtin@gmail.com> wrote:

Daniel,

I appreciate your help. I got drifted away yesterday and got back to it today. I found my mistake: a fricking typo. My polls/detail.html look for 'error_message' while I spelt 'error_messge' in my views.py. So the error msg was never shown though the page was redirected properly.

Thanks again!

On Monday, September 29, 2014 9:36:31 AM UTC-4, Daniel Rus Morales wrote:
No, that's not the cause, the lack of it would be. That line shows the content of 'error_message' when it does exist and has a value other than None. But if you have it in your template the error must be somewhere else in your code. Must be a simple syntax mistake you didn't notice, either in the template file or in the vote view.

To be sure that your code is raising an exception you can place a print statement right after the except, before the call to render. Replace the call to render with these lines, they do the same but also print a message in the console:

    except (KeyError, Choice.DoesNotExist):
        # redisplay the question voting form
        context = {
            'question': p,
            'error_message': "You didn't select a choice.",
        }
        print("context: ", context)
        return render(request, 'polls/detail.html', context)


Click vote again and see what's the output when you click on "Vote" without selecting any choice. The text of the print statement will show up in the console where you launched `python manage.py runserver`.

If you see the output "context: " and the 'error_message' string with the actual message, then you have a syntax mistake in your template. Otherwise it's in your view.

Cheers


On 29 Sep 2014, at 14:55, zaiks0105 <lah...@gmail.com> wrote:

I do. Is that line causing the behavior?



On Monday, September 29, 2014 7:15:47 AM UTC-4, Daniel Rus Morales wrote:
Hi Zaiks0105,

Do you have the following line in "your polls/templates/polls/detail.html"?

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

On 29 Sep 2014, at 12:48, zaiks0105 <lah...@gmail.com> wrote:

Hi,

I am following Django official tutorial and have unanswered issue at part 4, https://docs.djangoproject.com/en/1.7/intro/tutorial04/. Here is the exception handling code,

    except (KeyError, Choice.DoesNotExist):          # Redisplay the question voting form.          return render(request, 'polls/detail.html', {              'question': p,              'error_message': "You didn't select a choice.",          })

When I run the server and click on [Vote] without selecting a choice, the page does NOT show me the error message. The same voting page comes back as if [Vote] was not clicked.
I checked my lines and everything seems identical per tutorial.

Any help appreciated!
Thanks


-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69aaa1bb-5371-4aec-ae1f-fb44880e46b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2adefa94-7801-48ad-af1f-1d5ec10ada86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/64a09fab-f34d-4847-9643-07290910f7c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment