i have a quiz app, 4 answers per question, one correct via boolean.
For some reason I can't get the view to redirect to next question if
answer is correct.  Whenever I click on the correct answer the app
doesn't redirect to the next question.  Could anyone take a look at
this and see what I'm doing wrong?


--models.py--
from django.db import models

class Question(models.Model):
    problem = models.CharField(max_length=200)
    next = models.ForeignKey('self', null=True, blank=True)

    def __unicode__(self):
        return unicode(self.problem)

    class Meta:
        verbose_name    = 'question'
        verbose_name_plural  = 'questions'


    def get_absolute_url(self):
        return "/questions/%i/" % self.id

class Answer(models.Model):
    statement = models.CharField(max_length=200)
    question = models.ForeignKey(Question, related_name="answers")
    correct = models.BooleanField()

    def __unicode__(self):
        return unicode(self.statement)
--views.py--

def answer(request, question_id):
    q = get_object_or_404(Question, pk=question_id)
    a = q.answers.get(pk=request.POST['answer'])
    is_correct = a.correct
    if is_correct:
        return HttpResponseRedirect(q.next.get_absolute_url())
    else:
        return HttpResponseRedirect(q.get_absolute_url())
    return render_to_response('questions/question_detail.html', {
            'q': q,
            'a': a
    }, context_instance=RequestContext(request))
--question_detail.html--

<form action="/questions/{{ object.id }}" method="post">
{% for answer in object.answers.all %}
    <input type="button" name="answer"
value="{{ answer.statement }}" />
    {% if a.correct %}
        {{ q.next.get_absolute_url }}
    {% else %}
        {{ q.get_absolute_url }}
    {% endif %}
{% endfor %}
</form>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to