python - Database query in django -
i creating simple quiz app using django. when user submits answer question, answer checked using correct answer present in database.
views.py
from django.shortcuts import render,get_object_or_404 .models import question django.http import httpresponseredirect, httpresponse django.core.urlresolvers import reverse def detail(request, question_id): ques = get_object_or_404(question, pk=question_id) return render(request, 'detail.html', {'ques': ques}) def index(request): questions = question.objects.all() return render(request,"question.html",{"questions":questions}) def vote(request,question_id): p = get_object_or_404(question, pk=question_id) selected_choice = p.choice_set.get(pk = request.post['choice']) print selected_choice,p.answer_text if(selected_choice == p.answer_text): print "yes" x = int(question_id) + 1 return httpresponseredirect(reverse('detail', args=(str(x))))
the vote function called after user clicks submit button. check performed here. when print selected_choice , p.answer_text, show correct values. when compare them using equal operator, doesn't return true when both same.
models.py django.db import models class question(models.model): question_text = models.charfield(max_length = 400) answer_text = models.charfield(max_length = 400,default = "name") def __unicode__(self): return self.question_text,self.answer_text class choice(models.model): question = models.foreignkey(question) choice_text = models.charfield(max_length = 200) def __unicode__(self): return self.choice_text
Comments
Post a Comment