Django前端循环截取数据展示

制作一个网站时,网站原模版需要展示三张图片,我定义model时,定义相关图片来存储

class ApplicationRelatedImage(models.Model):
    image = models.ImageField(upload_to=application_directory_path, verbose_name="应用相关图片", blank=True,
                              null=True)
    application = models.ForeignKey(Application, on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.application.title

    class Meta:
        verbose_name = "应用相关图片"
        verbose_name_plural = verbose_name

在做前端显示时,循环调用即可展示图片,但是三张图片都有自己的样式,直接使用循环不能更改样式,可以使用forloop.counter来完成

{
    
    % for image in application.applicationrelatedimage_set.all %}
                    {
    
    % if forloop.counter == 1 %}
                        <li class="pull_left"><img src="{
    
    { image.image.url }}" width="291" height="197"
                                                   alt=""/>
                        </li>

                    {
    
    % endif %}
                    {
    
    % if forloop.counter == 2 %}
                        <li class="pull_right"><img src="{
    
    { image.image.url }}" width="212" height="197" alt=""
                                                    class="frist"/></li>

                    {
    
    % endif %}
                    {
    
    % if forloop.counter == 3 %}
                        <li class="pull_right"><img src="{
    
    { image.image.url }}" width="212" height="197" alt=""
                                                    class="frist"/></li>

                    {
    
    % endif %}
                {
    
    % empty %}
                    <div class="no-post">暂时还没有banner图片!</div>
                {
    
    % endfor %}

参考链接

Django前端循环截取数据展示

猜你喜欢

转载自blog.csdn.net/cll_869241/article/details/120032854