Poll’s choices are not showing while accessing from view in template

Solution for Poll’s choices are not showing while accessing from view in template
is Given Below:

I am building a Poll App and I am stuck on an Problem.

What i am trying to do :-

I am trying to access all three choices of poll from view in template BUT only one choices is showing. BUT when i access Poll object in view and access choice model from template then all three choices are successfully showing.

models.py

class Poll(models.Model):
    user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
    title = models.TextField()

    def get_absolute_url(self):
        return reverse('detail_poll', kwargs={'pk': self.pk})

class Choice(models.Model):
    poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=30)

forms.py

class PollAddForm(forms.ModelForm):
    choice1 = forms.CharField(label="Choice 1",max_length=100,min_length=2)
    choice2 = forms.CharField(label="Choice 2",max_length=100,min_length=2)
    choice3 = forms.CharField(label="Choice 3",max_length=100,min_length=2)

    class Meta:
        model = Poll
        fields = ['title','choice1', 'choice2', 'choice3']

I am increasing choices from forms.

views.py


def detail_poll(request,poll_id):
    poll = get_object_or_404(Poll, id=poll_id)

    for choice in poll.choice_set.all():
        printChoice = choice.choice_text

    context = {
        'printChoice ':printChoice ,
    }

    return render(request, 'detail_poll.html',context)

In view i am accessing all the choice from choice_text of the poll.
I am accessing three choices for vote with the same (choice_set) method in template.

AND When i create poll then poll is successfully saving with all three choices. When i vote then poll is successfully voting with choices.

BUT when i accessing the choices to calculate percentage from view then choices are not showing.
With the same method of poll.choice_text.all in template, it does work but not from view.

Any help would be much Appreciated.

Thank You in Advance.

It’s only showing one choice because you are sending only one choice to context. i.e the last choice. Check your view thoroughly. When the for-loop stops, printChoice will have the last choice and you are sending that to context. So only one choice will be shown in template.

You should iterate over the choices and save them to a data structure like a dict, set, list etc, and then send it to the context.

It should be like this. I have used a list to store the choice_texts and pass it to context.

def detail_poll(request,poll_id):
    poll = get_object_or_404(Poll, id=poll_id)
    choice_set = []
    for choice in poll.choice_set.all():
        choice_set.append(choice.choice_text)
     
   # You can use your percentage calculation here...

    context = {
        'printChoice ': choice_set ,
    }

    return render(request, 'detail_poll.html',context)

You can also send the entire queryset to context like this.

context = { 'printChoice': poll.choice_set.all() }

And then in template, show the choice_text like this

{% for choice in printChoice %} 
  <p>choice.choice_text</p>
{% endfor %}