如何在django模板中申请循环来创建相同图像的数组?

时间:2018-01-27 12:04:44

标签: python html django django-templates

我需要没有javascript的django模板中的代码来显示基于投票数的相同图像的长度。我目前的代码是:

<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. 基于choice.votes的循环增量。 // for i from i = 0; I&LT; {{choice.votes}};我+ +
  2. 在数组中显示某些图像。 //一些图片
  3. 这是我的models.py

    来自django.db导入模型

    # 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
    

0 个答案:

没有答案