I’ve gone through part 4 of the Django-Polls tutorial, and I’ve noticed that the vote buttons are missing when I click any of the four questions I’ve created. The webpages are working, except that it’s missing the choice vote buttons. I copied the detail.html codes directly from Django’s tutorial page.
Please post your complete detail.html file (When posting code, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your template, then another line of ```.)
#detail.html
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
<input type="submit" value="Vote">
</form>
Can you confirm that this is the page that isn’t displaying what you’re expecting to see? (If this isn’t the page, which page isn’t working?)
You’re saying that when this page is rendered that you don’t have the radio buttons showing? (If this is the page giving problems, can you post a screenshot image showing what’s being displayed?)
Also, you mentioned that you had created some questions. Did you also create choices for those questions? (If there are no choices, then there won’t be any buttons for selecting them.)
This is the page that is missing the vote buttons.
I created those choices in the app models.py file
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
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
Ok, so I can see that you created the question.
Did you create any choices for that question?
I created the choices in the app’s models.py file.
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
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
You created a model named Choices
- but that’s not what I’m asking about.
Did you create any instances of Choices
for the question A new question?
?
(In the samples, it’s done as part of page 2 of the tutorial.)
Yes, I created them in the Python Shell.
# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
<QuerySet []>
# Create three choices.
>>> q.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>
>>> q.choice_set.create(choice_text='The sky', votes=0)
<Choice: The sky>
>>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)
The vote button only showed up in the pk 1 record and not in the other Question records.
Because that’s the Question object you created those Choices for.
Each Question has a separate set of Choices for that question.
Hmm. So I have to create choices for each question record created? How does one create choices globally for all question records created? I didn’t encounter in the Django tutorial. Or will I encounter that past part 5 of the tutorial?
Yes, you do.
One doesn’t.
What those models are and how they’re related to each other is specified in the creating-models section of part 2.