gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:23:47 24 4
gpt4 key购买 nike

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

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

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

这是回溯(最后一行):

 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com