python - 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
感谢一切!希望这是可以理解的。(:
解决方案
你刚刚忘记了
{% for choice in question_set.all %}
推荐阅读
- kubernetes - 如何在 Kubernetes 中将 VPA 与数据库 pod 一起使用?
- android - 使用 Drive API 将图像上传到 Google Drive
- javascript - Flask 的 SocketIO 没有向我的客户发送正确的脚本
- aws-lambda - AWS lambda:没有名为“电报”的模块
- sql-server - 将 SQL Server 查询结果导出到带有双引号的 .csv 文件时,列中出现逗号问题,数据正在拆分到下一列
- javascript - 使用 cPanel 在 GoDaddy 共享主机上部署 Sapper 应用程序
- javascript - 在 chrome 扩展中使用 Materialize 库
- reactjs - 在 REACT JS 中处理时刻(未定义)的干净方法
- jquery - ajax 获取请求超时未触发错误处理程序
- javascript - Vue.js 路由器链接参数未定义