Python 如何在模板中显示所有用户配置文件图片

Python 如何在模板中显示所有用户配置文件图片,python,django,Python,Django,我已经能够在配置文件中添加一个图像字段,并且我创建了用户,并将一个图像附加到我的所有用户配置文件中。现在,如何在模板中显示所有用户的配置文件图片。我尝试这样做:profile\u img=profile.objects.filter(user=request.user.id),但它在所有用户配置文件图片中显示当前登录用户配置文件图片。我想在模板中显示每个用户的所有配置文件图片 class Profile(models.Model): user = models.ForeignKey(se

我已经能够在配置文件中添加一个图像字段,并且我创建了用户,并将一个图像附加到我的所有用户配置文件中。现在,如何在模板中显示所有用户的配置文件图片。我尝试这样做:profile\u img=profile.objects.filter(user=request.user.id),但它在所有用户配置文件图片中显示当前登录用户配置文件图片。我想在模板中显示每个用户的所有配置文件图片

class Profile(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
    profile_pic = models.ImageField(upload_to='ProfilePicture/', blank=True)

def home(request):
    profile_img = Profile.objects.filter(user=request.user.id) 
    print(profile_img)
    users = User.objects.exclude(id=request.user.id)

{% for user in users %}
<div class="row mb-2">
    {% for profile in profile_img %}
    {% if profile.profile_pic %}
      <a href="{% url 'site:profile' user.username %}">  
      <img src="{{ profile.profile_pic.url }}" class="rounded-circle avatar-img z-depth-1-half mb-3 mt-1" height="55" width="55" style="border:none;padding:0px;">
      </a>
    {% endif %}
    {% endfor %}
    </div>
    {% endfor %}
类配置文件(models.Model):
user=models.ForeignKey(settings.AUTH\u user\u MODEL,on\u delete=models.CASCADE,null=True,blank=True)
profile_pic=models.ImageField(上传到='ProfilePicture/',blank=True)
def home(请求):
profile\u img=profile.objects.filter(用户=request.user.id)
打印(配置文件\图像)
users=User.objects.exclude(id=request.User.id)
{users%%中的用户为%s}
{配置文件中的配置文件为%u img%}
{%if profile.profile_pic%}
{%endif%}
{%endfor%}
{%endfor%}

我认为,在视图中,您是通过
request.user.id
对其进行过滤的(这就像您只想根据用户id发送用户配置文件pic)。相反,请尝试
Profile.objects.all()
(这将发送其中所有配置文件的图像)

如果要按顺序排列,请使用
Profile.objects.all().order_by('-id')
。这将按用户id排序

要排除当前用户的图像,请使用以显示所有图像

Profile.objects.exclude(id=request.user.id)
def home(请求):
profile\u img=profile.objects.exclude(id=request.user.id)
打印(配置文件\u img)


{配置文件中的配置文件为%u img%}
{%if profile.profile_pic%}
{%endif%}
{%endfor%}

我认为没有必要添加for用户循环,因为您排除了当前用户,并将其他两个对象保存在名为users的变量中,这样它将循环两次并再次打印相同的内容避免for用户循环。你应该只获得一次图像

@Gokulaselvan…谢谢你说得对,我也实现了这样的功能,但问题是登录用户配置文件图片仍然显示在列表上。如何仅排除登录用户配置文件图片?例如,我有3个用户(John、Mike、Paul)。当John登录时,我不希望John的个人资料图片仅显示Mike和Paul。然后使用profile.objects.exclude(id=request.user.id)。这可能会给您预期的结果,即排除当前用户id并显示所有图像。为什么用户的个人资料图片显示如下:(Mike img,Paul img)(Paul img,Mike img)。而不是(迈克·伊姆格,保罗·伊姆格)。这应该是模板代码的原因吗?或者distinct()应该是处理此问题的最佳方法?@GistDomBlog共享您添加和输出的代码。我无法通过添加模板代码来更新我的问题。@Gokulaselvan..users=User.objects.exclude(id=request.User.id)显示模板中的所有用户列表。如果我没有为用户中的用户添加一个循环,我如何才能获得模板中的所有用户列表?而且,如果我删除了用户的循环,那么我就无法创建指向用户配置文件页面的href链接。你对我如何处理这个问题有什么建议吗?@GistDomBlog你不需要创建用户变量,你可以直接在profile\u img中使用exclude,正如我在回答中提到的。该profile\u img已经有两个对象了,但当前用户除外。现在使用profile\u img for循环,您将获得if语句中每个对象的profile pic。。。。。。。。因此,请使用上面的代码再试一次。如果您的代码工作正常,它将只显示具有个人资料图片的用户。这很好。但现在的问题是url不能反向到其他用户页面。当我在用户中为用户循环时,我使用这个url“site:profile”user.username,但现在我在profile\u img中为profile使用循环,我将我的url更改为“site:profile”profile.username。这不会将我重定向到profile,它会为未找到关键字参数{'username}的profile显示反向。我在url中使用的用户名不是ID。@GistDomBlog请尝试使用“site:profile”user.ID并在models.py中添加get\u absolute\u url。这可能会解决url问题。不要在模型中反转或使用主键(pk),而不是id@Gokulaselvan..this“site:profile”user.id不会将我带到其他用户的配置文件页面,它只会将我重定向到已登录的用户配置文件页面(已登录的用户)。我的意思是,我想点击一个链接,将我带到其他用户的个人资料页面。我是否必须为所有用户创建单独的用户配置文件页面?
<div class="row mb-2">
{% for profile in profile_img %}
{% if profile.profile_pic %}
  <a href="{% url 'site:profile' user.username %}">  
  <img src="{{ profile.profile_pic.url }}" class="rounded-circle avatar-img z-depth-1-half mb-3 mt-1" height="55" width="55" style="border:none;padding:0px;">
  </a>
{% endif %}
{% endfor %}
</div>