如何在Django模板中应用循环来创建相同图像的数组?

2024-04-18 05:49:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在django模板没有javascript的代码需要显示相同的投票数为基础的图像长度。我现在的代码是:

<div id="Results">
    <h1>{{question.question_text}}</h1>
    {% for choice in question.choice_set.all %}
    <div id="votes">
        <img style="border-radius: 2px; border-width: 2px; border-style: none solid solid none; border-color: darkblue;" src='{{choice.image2.url}}'
        />
        <div style=" float: right; width: 88%;">
            <b>{{choice.choice_text}}</b>
            {{choice.votes}} vote{{choice.votes|pluralize}}
        </div>
    </div>
    {% endfor %}
    </ul>
    <p id="info">Your Vote Has Been Stored!!</p>
    <br>
</div>

帮助我生成算法代码:

  1. 循环增量基于选择。投票. //对于来自i=0的循环;i<{{选择。投票}};i++
  2. 在数组中显示某些图像。//一些图像

这是我的型号.py你知道吗

从django数据库导入模型

# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length= 100)
    pub_date = models.DateTimeField('Date Is Published')
    image = models.ImageField(upload_to="Question_Image", blank=True)

    def __str__(self):
        return self.question_text

class Choice(models.Model):
    choice_text = models.CharField(max_length= 200)
    votes = models.IntegerField(default= 0)
    image2 = models.ImageField(upload_to="Question_Image2", blank=True)
    question = models.ForeignKey(Question, on_delete= models.CASCADE)

    def __str__(self):
        return  self.choice_text

Tags: django代码text图像selfdividstyle