无法在模板中显示图像(Django 3.0)

时间:2020-05-05 20:16:43

标签: django django-models django-views django-templates

无法在模板中显示profile_pic图片

我知道我做的事情很糟糕。

model.py

class UserProfileInfo(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)

portfolio_site = models.URLField(blank=True)
profile_pic = models.ImageField(upload_to='basic_app/profile_pics',blank=True)

def __str__(self):
    return self.user.username

index.html

{% block body_block %}

<div class="container">
    <div class="jumbotron">
        <h1>Django Level Five</h1>
        {% if user.is_authenticated %}
            <h2>Welcome {{ user.username }}!</h2>
            <div>
                <img src="{{ user.profile_pic.url }}" alt="user-image">
            </div>

        {% else %}
            <h2>Welcome to the site!</h2>
            {% for b in users %}
            {% endfor %}
        {% endif %}
    </div>
</div>{% endblock %}

views.py

def index(request):
   #commented: userpro = UserProfileInfo()
   #commented: var={'userz': userpro}
   #commented: return render(request, 'basic_app/index.html',context=var)

   return render(request, 'basic_app/index.html')

用户身份验证正常,并且名称显示在模板中,而不显示图像。我已经尝试了很多东西,但是没有显示图像。显示Div和Alt-name,但不显示profile_pic。

1 个答案:

答案 0 :(得分:0)

用户对象没有profile_pic字段。

尝试使用:

user.userprofileinfo.profile_pic.url

相关问题