python - Django - 'for' 语句应该至少有四个字 : for choice question_set. all

标签 python django for-loop syntax

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

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

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

这是回溯(最后一行):

 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

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

最佳答案

你刚刚忘记了中的声明

{% for choice in question_set.all %}

关于python - Django - 'for' 语句应该至少有四个字 : for choice question_set. all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50124863/

相关文章:

Python文件关键字参数?

python - 从模型更新 UI 组件

python - jinja2 中带有 html 标签的 Django 模板

javascript - Django ajax 403 因为 httponly cookie

c++迭代后破坏对象的问题

vba - 如何使 For-Each 循环向后运行

python - Django 部署 https + gunicorn 和 nginx

python - Django模板不能循环defaultdict

java - 是否每次都在for循环的condition语句中执行函数?

python - 我可以使用 np.resize 来填充带有 np.nan 的数组吗