In django ,display all user with their corresponding profile picture on admin home page
I am new in django, I am working on a project I want to display users information with their corresponding profile picture on admin home page. I am using default User Model.
I use userdata= User.objects.filter(is_superuser=False, is_staff=False) to display all user informations
my Profile model is
class Profile(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
forget_password_token = models.CharField(max_length=100)
image = models.ImageField(upload_to="images",default="default/user.png")
def __str__(self):
return f'{self.user} profile'
i want to do like this enter image description here
Please help me to achieve this. Thanks in advance
I am trying to achieve this from default user model
id | image | name
profile1.jpg john
profile2.jpg poul
profile3.jpg mitch
I have done enter image description here
In the profile column I want profile image of corresponding user
i want to achieve this in the profile column I want to display user profile picture
You are not able to query is_superuser=False and not able to display image because you are filtering through inbuilt User Model not your Profile Model, which has better relationship i.e. OneToOne relation with your User Model. You can simply query through Profile Model and get your work done in following way:
Try below code:
models.py
class Profile(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
forget_password_token = models.CharField(max_length=100)
image = models.ImageField(upload_to="images",default="default/user.png")
def __str__(self):
return f'{self.user} profile'
views.py
from django.shortcuts import render
from . models import Profile
from django.db.models import Q
def showallusers(request):
data = Profile.objects.filter(Q(user__is_superuser=False), Q(user__is_staff=False))
return render(request, "instructor/showallusers.html", {'data': data})
In the above view query is done through Profile model.
Template file
<thead>
<tr style="background-color:gray;">
<th scope="col">ID</th>
<th scope="col">Profile</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for userData in data %}
<tr>
<th scope="row">{{userData.id}}</th>
<td><img style="width: 40px; border-radius:15px;" src="{{userData.image.url}}" alt="this is it."></td>
<td>{{userData.user.first_name}}</td>
<td>{{userData.user.last_name}}</td>
<td>{{userData.user.email}}</td>
<td>
<a href="{% url 'dashboard:delete_user' userData.id %}"> <button class="btn btn-danger btn-sm" onclick="window.mytest()">Delete</button></a>
<script type="text/javascript">window.mytest = function() { var isValid = confirm('Are you sure to Delete this user?');
if (!isValid) { event.preventDefault(); alert("It wont delete. Yay!");}}</script>
</td>
</tr>
{% endfor %}
</tbody>
Note:TheuserData.idwhich is passed ashref="{% url 'dashboard:delete_user' userData.id %}"to be deleted, will take theidfromProfilemodel.
Note:Thefunctionbased views in django are written insnake_caseso it would be better if the name of view changed toshow_all_usersfromshowallusers.