I am trying to modify the polls example from djangoproject.com to take
a integer point value for each of the 1 thru 5 poll items instead of
having a radio button where u select one. However, I am having
problems with the views.py syntax. I changed the Choice obejct to be
a Song object and changed the name of the choice and votes properties
to be name and points respectively. I keep getting this annoying
"need more than 1 value to unpack" error. Below are some snippets of
code. Please can someone help me out with the views syntax, or do I
need to create a ModelForm/form to deal with getting the input to the
views.
Thanks in advance,
Seamus
views.py
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from mysite.polls.models import Poll, PollForm, Song, SongForm
def vote(request, poll_id):
p = get_object_or_404(Poll, poll_id)
try:
#selected_song_set = p.song_set.get(pk=request.POST['song'])
selected_song_set = p.song_set.get(pk=poll_id)
except (KeyError, Song.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('polls/poll_detail.html', {
'object': p,
'error_message': "You didn't select a choice.",
})
else:
# need to submit
# selected_song_set.points += 1
selected_song_set.points1 = points1
selected_song_set.points2 = points2
selected_song_set.points3 = points3
selected_song_set.points4 = points4
selected_song_set.points5 = points5
selected_song_set.save()
# Always return an HttpResponseRedirect after successfully
dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
#return
HttpResponseRedirect(reverse('mysite.polls.views.results',
args=(p.id,)))
return HttpResponseRedirect(reverse('poll_results',
args=(p.id,)))
models.py:
from django.db import models
from django.forms import ModelForm
import datetime
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Song(models.Model):
poll = models.ForeignKey(Poll)
name = models.CharField(max_length=200)
points = models.IntegerField()
#points = models.IntegerField(min_value='1', max_value='5')
def __unicode__(self):
return self.song
poll_detail.html
<h1>{{ object.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{%
endif %}
<form action="/polls/{{ object.id }}/vote/" method="post">
{% for song in object.song_set.all %}
<input type="text" name="song{{ forloop.points }}" id="song
{{ forloop.counter }}" />
<label for="song{{ forloop.counter }}">{{ song.name }}</
label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
results.html:
<h1>{{ object.question }}</h1>
<ul>
{% for song in object.song_set.all %}
<li>{{ song.name }} -- {{ song.points }} points{{ song.points|
pluralize }}</li>
{% endfor %}
</ul>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---