vote - Django Polls Tutorial with Voterid per choice stored -
hi have created django voting app tutorial. looking add when loggedin user votes choice, choice section stored db. models.py
class choice(models.model): question = models.foreignkey(question, on_delete=models.cascade) choice_text = models.charfield(max_length=400) vote = models.integerfield(default=0) points = models.integerfield(default=1) def __str__(self): return self.choice_text class voter(models.model): user = models.foreignkey(user) selections = models.charfield( 'question.choice', max_length=600)
my views.py , vote.view:
class voteview(generic.view): def dispatch(self, request, *args, **kwargs): # getting current question question = get_object_or_404(question, pk=kwargs.get('question_id')) try: selected_choice = question.choice_set.get(pk=request.post['choice']) except (keyerror, choice.doesnotexist): # display flash message messages.error(request, "you didn't select choice.") # redirect current question voting form again return httpresponseredirect(reverse('questionaire:detail', args=(kwargs.get('question_id'),))) else: selected_choice.vote += 1 selected_choice.save() v = voter(user=request.user, question=q) v.save()
so trying save choices selected per users database, stored later processing , analysis.
you can add question
, choice
model voter
foreignkey
fields.
you have question
fk in voter
didn't write here.
also, consider renaming model voter
vote
Comments
Post a Comment