Skip to main content
 首页 » 编程设计

python之图像未显示在 django 模板中

2024年08月16日34zhenyulu

我在 Django 模板中显示图像时遇到问题。除了 imagefield 其他字段显示正确。请在下面找到模板和模型。请注意,我没有在我的模型中声明任何图像字段。任何帮助都会很大赞赏。

模型.py

class Any(models.Model): 
    Status=models.CharField(max_length=10, 
                            choices=Status_choices,default='SUCCESS') 
    Smoke=models.CharField(max_length=10, 
                           choices=Smoke_choices,default='SUCCESS') 
    Sanity=models.CharField(max_length=100, 
                            choices=Sanity_choices,default='YES') 

模板:

<table class = "table table-striped table-bordered"> 
    <thead> 
            <tr> 
              <th>Tag</th> 
              <th>Smoke</th> 
              <th>Status</th> 
              <th>Sanity</th>                  
            </tr> 
    </thead>          
{% for result in results %}  
   <tbody> 
    <tr>  
        <td> 
            {% if result.Status == SUCCESS %}<img src='/static/test/img/release.png' class="img-fluid rounded" alt="Generic placeholder thumbnail" width="20" height="20"/>                      
            {% elif result.Status == FAILURE %} <img src='/static/test/img/block.png' class="img-fluid rounded" alt="Generic placeholder thumbnail" width="20" height="20"/> 
            {% elif result.Sanity == NO %}<img src='/static/test/img/thumbs-down.png' class="img-fluid rounded" alt="Generic placeholder thumbnail" width="20" height="20"/> 
       {% endif %} </td>  
        <td>{{ result.Smoke }}</td> 
        <td>{{ result.Status }}</td> 
        <td>{{ result.Sanity }}</td>           
    </tr> 
   </tbody> 
{% endfor %}    

请您参考如下方法:

对于static 图像,您需要像下面那样使用src。

{% load static %} 
 
<img src="{% static 'test/img/release.png' %}" class="img-fluid rounded" alt="Generic placeholder thumbnail" width="20" height="20"/>