作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
一切都好吗?这是我的第一个问题,英语不是我的母语所以请放轻松哈哈哈哈
好吧,这就是问题所在,我相信这是一个身份...不知道用英语怎么说,但无论如何,结构问题。
发生了什么:我正在按照教程进行学习,在我的本地主机网站上几乎所有内容都运行正常,但是当我决定对“问题”进行“投票”时,它会导致出现错误页面。
这是回溯(最后一行):
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/
一切都好吗?这是我的第一个问题,英语不是我的母语所以请放轻松哈哈哈哈 好吧,这就是问题所在,我相信这是一个身份...不知道用英语怎么说,但无论如何,结构问题。 发生了什么:我正在按照教程进行学习,在我
我是一名优秀的程序员,十分优秀!