There is no need to add the Choices
to the context. You add the Question
queryset and inside the template you render the associated Choices
:
def func_allreslt(request):
context = {}
context["questions"] = Question.objects.all()
return render(request, "polls/funcallreslt.html", context)
And inside your template:
<ul>
{% for question in questions %}
<li> {{question.question_text }} </li>
<ol type=1>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ol>
{% endfor %}
</ul>
This is based on the Django Tutorial (https://docs.djangoproject.com/en/3.0/intro/tutorial01/) to build a “Polls” app.
It’s essentially a Question model and a Choice model. The Choice model is linked to Question through a foreign key (named ‘question’).
After completion of the tutorial, I thought I’d try to add a specific functionality to the app:
On a single html page, display a list of all the questions, with their associated choices underneath each question. (Displaying the number of votes for each choice as well would be a plus but not necessary at this point.)
Something like this would be an ideal page display.
Right now, I can display ALL questions (with NO choices) on a page
-OR-
ONE question with ALL associated choices on a page.
I cannot seem to display ALL the questions AND their choices on a single page.
The below is based on the similar questions and responses I could find as well as the djangoproject.com documentation.
models.py:
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
<...etc: additional code here about pub_date...>
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
To populate the queryset with the instances I want, I’ve tried both a Class-based view (& variations), and a Function-based view (& variations).
(I have the least confidence in the Class-based attempt. I’ll post it if anyone wants to see it.)
The Function-based view approach:
view.py:
def func_allreslt(request):
context = {}
context["question"] = Question.objects.all()
context["choice"] = Choice.objects.all()
return render(request, "polls/funcallreslt.html", context)
funcallreslt.html:
<ul>
{% for question in context %}
<li> {{question.question_text }} </li>
<ol type=1>
{% for choice in context %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ol>
{% endfor %}
</ul>
I’ve also tried using various combinations of prefetch_related and/or select_related. I can offer that code if anyone’s interested.
(I’m still using .all() without any filtering or slicing because the db currently holds 4 questions and about 13 choices total, so I didn’t figure it was necessary to cut down on what’s being retrieved yet.)
Can anyone point me in the right direction to accomplish this?
(After this I thought I might go through and build a series of if/then statements to see if I could list the results through that logic.
Another road I thought about was writing it out as an SQL query and then seeing if I could get Django to use the results.)