Wednesday, January 23, 2013

Django Tutorial on Hostgator part 4

I went through part 4 of the Django tutorial last night.  The mix up between the dev and 1.4 version caught me again.  It caused multiple problems and what I felt should have taken an hour took most of the evening.  The problem was found after creating results.html and testing the application.  After hitting the vote button this error comes up:  Reverse for 'polls.views.results' with arguments '(1L,)' and keyword arguments '{}' not found.


It appeared that a lot of people had the problem [1] [2] [3].  I tried the suggestions in the posts and tweaked code to no avail.   After spending an hour I decided that I had to go back to the start of the tutorial and check everything. What I found was that in polls/models.py file the Choice class had a variable change names between the 1.4 version and the dev version.  The variable was called choice in 1.4 and choices_text in dev.  So in the mix up of using different documentation at different times both were used in the code.   I decided since the database was built using choices_text that I would go through and change anywhere I used the invalid choice attribute and update it to choices_text.


I ended up changing it 2 times:


in detail.html from:
<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />

to:
<label for="choice{{ forloop.counter }}">{{ choice.choices_text }}</label><br />


in results.html from:
<li>{{ choice.choice}} -- {{ choice.votes }} vote {{ choice.votes|pluralize }}</li>

to:
<li>{{ choice.choices_text}} -- {{ choice.votes }} vote {{ choice.votes|pluralize }}</li>

I also had an error in polls/view.py in the vote function which I changed to:
return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))

After that I had a working poll that I could vote on!  I plan on sharing all of the code from this on github if anyone is interested.  I will share that link when it is available.

No comments:

Post a Comment