On Tue, 2009-01-20 at 13:08 -0800, saved...@gmail.com wrote: > 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())
Given that one of the above two lines are always going to be executed (either the "if" branch or the "else" branch), the code that follows is pointless. It will never be run as one of the above two return statements will return from the function. If you've edited your code to simplify things, you've removed some relevant details. > return render_to_response('questions/question_detail.html', { > 'q': q, > 'a': a > }, context_instance=RequestContext(request)) This is the bit that will never be run. The questions I would have when debugging this are: (a) what does "is_correct" contain when you think you should be entering the "correct" branch? Is it really something that will evaluate to True in Python? (b) What does q.next.get_absolute_url() return, if so? If, for example, you were returning an empty string, things could behave strangely. A bit of debugging along those sorts of lines. Working out what you expect to see in the variable contents and whether that matches reality, would be a good start. Just drop in some print statements and see what they say. Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---