如何在管理主页上显示所有用户及其相应的个人资料图片?

How to display all user with their corresponding profile picture on admin home page?

我正在做一个项目,我想在管理员主页上显示用户信息及其相应的个人资料图片。 我正在使用默认用户模型。

我用userdata= User.objects.filter(is_superuser=False, is_staff=False)显示所有用户信息。它显示了我想要的用户信息,但我的问题是我无法显示用户对应的个人资料图片

资料模型:

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代码

def showallusers(request):
data= User.objects.filter(is_superuser=False, is_staff=False)
return render(request,"instructor/showallusers.html",{'data':data})

setting.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

urls.py

if settings.DEBUG:
 urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我的用户显示模板是

<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>here I want profile image of corresponding users</td>
  <td>{{userData.first_name}}</td>
  <td>{{userData.last_name}}</td>
  <td>{{userData.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>

我想这样做 this

在个人资料栏我想显示相应用户的个人资料图片

您无法查询 is_superuser=False 并且无法显示图像,因为您是通过内置 User 模型而不是您的 Profile 模型进行过滤,后者具有更好的关系,即 OneToOne 与您的 User 模型的关系。您可以简单地通过 Profile 模型查询并按照以下方式完成您的工作:

试试下面的代码:

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})

上面的视图查询是通过Profile模型完成的。

模板文件

<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: The userData.id which is passed as href="{% url 'dashboard:delete_user' userData.id %}" to be deleted, will take the id from Profile model.

Note: The function based views in django are written in snake_case so it would be better if the name of view changed to show_all_users from showallusers.