大家好,我在Django HTML中有一个查询。我在呼叫一个图像来自HTML中的模型,但它没有反映代码已列出低于
下面是型号
class UserProfileInfo(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE, related_name='users1')
portfolio_site = models.URLField(blank=True)
bio = models.CharField(max_length=2000)
profile_pic = models.ImageField(upload_to='profile_pics', blank=True)
def __str__(self):
return self.user.username
以下是视图文件
class UserDetailView(DetailView):
context_object_name = 'users_detail'
model = models.UserProfileInfo
template_name = 'blog/user_detail.html'
下面是Html文件
{% extends "blog/base.html" %}
{% load static %}
{% block content %}
<div class="container">
<h1>Welcome</h1>
<table>
<thead>
<th>User Name</th>
<th>portfolio_site</th>
<th>bio</th>
<th>profile_pic</th>
</thead>
<tr>
<td>{{ users_detail.user }}</td>
<td>{{ users_detail.portfolio_site }}</td>
<td>{{ users_detail.bio }}</td>
<td><img src="media/{{ users_detail.profile_pic }}" width ="300" alt="">media/{{ users_detail.profile_pic }}
</td>
</tr>
</table>
<p></p>
<p><img src="media/profile_pics/denny.jpg" alt="hiiii">ho</p>
</div>
{% endblock %}
使用.url
属性访问文件或图像字段的正确URL
<td><img src="{{ users_detail.profile_pic.url }}" width="300" alt="">{{ users_detail.profile_pic.url }}</td>