首页 > 解决方案 > Django - 'for' 语句应该至少有四个词:for selection question_set.all

问题描述

一切都好吗?这是我的第一个问题,英语不是我的母语,所以请放轻松哈哈哈哈

好的,这就是问题所在,我相信这是一个身份... idk 用英语怎么说,但无论如何,结构问题。

发生了什么:我正在按照教程学习,并且在我的本地主机网站上几乎所有内容都运行良好,但是当我决定对“问题”进行“投票”时,它会导致错误页面。

这是回溯(最后一行):

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

以及放置错误(我相信)的位置,我的“/results.html”文件:

<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>

我的models.py代码(如要求<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

感谢一切!希望这是可以理解的。(:

标签: pythondjangofor-loopsyntax

解决方案


你刚刚忘记

{% for choice in question_set.all %}

推荐阅读