Python – Django – ‘for’ statements should have at least four words : for choice question_set. all

Django – ‘for’ statements should have at least four words : for choice question_set. all… here is a solution to the problem.

Django – ‘for’ statements should have at least four words : for choice question_set. all

Is everything okay? This is my first question, English is not my native language so please take it easy hahahaha

Well, that’s the problem, I believe it’s an identity… Not sure how to say it in English, but anyway, structural issues.

What happened: I was following the tutorial and almost everything worked fine on my localhost site, but when I decided to “vote” on the “issue”, it resulted in an error page.

This is the traceback (last line):

 django.template.exceptions.TemplateSyntaxError: 'for' statements should have at least four words: for choice question_set.all

Location of the error (I believe), my “/results.html” file:

<h1>{{ question.question_text }}</h1>

<ul>
  {% for choice question_set.all %}
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ 
choice.votes|pluralize }}</li>
  {% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

My models.py code (as described in <3):

from django.db import models
import datetime
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

def was_published_recently(self):
    now = timezone.now()
    return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'

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

Thank you for everything! Hope this is understandable. (:

Solution

You just forgot the statement in

{% for choice in question_set.all %}

Related Problems and Solutions