Django Class 基于视图的个人资料视图,无法区分模板中的登录用户和正在修改的用户

Django Class Based Views Profile view, can't distinguish between user logged in and user being modified in a template

我正在尝试创建一个用户仪表板,我要实现的第一件事是用户配置文件。所有的个人资料都应该是 public,如果用户正在访问他们自己的个人资料,我想添加一个编辑按钮。我的问题是,当我进入某人的页面时,它会将 user 变量替换为我正在查看个人资料的用户。

我的url:

url(r'^profile/(?P<pk>\d+)$',
    views.ProfileView.as_view(),
    name='profile'),

我创建了一个视图:

from django.contrib.auth import get_user_model()
from django.views.generic.detail import DetailView

class ProfileView(DetailView):
    model = get_user_model()
    template_name = 'accounts/profile.html'

在我的模板中:

{% if user == object %}user and object are the same{% endif %}

当当前用户在他们自己的个人资料上时,我看到 user and object are the same,但当当前用户在查看另一个个人资料时它也有效。我错过了什么吗?为什么他们是一样的?

user 变量由 django.contrib.auth.context_processors.auth 上下文处理器注入。

要解决此问题,请将 context_object_name 设置为非 "user" 字符串:

class ProfileView(DetailView):
    model = get_user_model()
    context_object_name = 'user_object'
    template_name = 'accounts/profile.html'

然后在模板中使用这个名字:

{% if user == user_object %}user and object are the same{% endif %}