Image files not importing Using Django

Viewed 20

Hello Guys, I have one query in Django HTML. I was calling an image from the model in the HTML But it is not reflecting The code is listed below

the below is the Models

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

the below is the Views files

   class UserDetailView(DetailView):
        context_object_name = 'users_detail'
        model = models.UserProfileInfo
        template_name = 'blog/user_detail.html'

the below is the Html File

{% 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 %}
1 Answers

Use the .url attribute to access the correct URL for a file or image field

<td><img src="{{ users_detail.profile_pic.url }}" width="300" alt="">{{ users_detail.profile_pic.url }}</td>
Related