简体   繁体   English

如何在Django中进行用户的DetailView?

[英]How to do a DetailView of Users in Django?

I currently have a DetailView for Django's built-in User . 我目前有一个用于Django内置UserDetailView

url(
    r'^users/(?P<pk>\d+)/$',
    DetailView.as_view(
        model = User,
        template_name = 'doors/users/detail.html'
    ),
    name = 'users_detail'
)

But when I access user in the template, it brings up the current logged in user, not the user with the pk that I pass from DetailUser . 但是当我在模板中访问user时,它会调出当前登录的用户,而不是具有我从DetailUser传递的pk的用户。 Do I need to tell DetailUser to rename the user variable to something else? 我是否需要告诉DetailUseruser变量重命名为其他内容? And if so, how do I do that? 如果是这样,我该怎么做?

The django.contrib.auth.context_processors.auth sets the {{ user }} template context variable to either request.user or AnonymousUser. django.contrib.auth.context_processors.auth将{{ user }}模板上下文变量设置为request.user或AnonymousUser。 So, it overrides the {{ user }} context variable created by your DetailView: 因此,它会覆盖DetailView创建的{{ user }}上下文变量:

def auth(request):
    """
    Returns context variables required by apps that use Django's authentication
    system.

    If there is no 'user' attribute in the request, uses AnonymousUser (from
    django.contrib.auth).
    """
    # If we access request.user, request.session is accessed, which results in
    # 'Vary: Cookie' being sent in every request that uses this context
    # processor, which can easily be every request on a site if
    # TEMPLATE_CONTEXT_PROCESSORS has this context processor added.  This kills
    # the ability to cache.  So, we carefully ensure these attributes are lazy.
    # We don't use django.utils.functional.lazy() for User, because that
    # requires knowing the class of the object we want to proxy, which could
    # break with custom auth backends.  LazyObject is a less complete but more
    # flexible solution that is a good enough wrapper for 'User'.
    def get_user():
        if hasattr(request, 'user'):
            return request.user
        else:
            from django.contrib.auth.models import AnonymousUser
            return AnonymousUser()

    return {
        'user': SimpleLazyObject(get_user),
        'messages': messages.get_messages(request),
        'perms':  lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
    }  

You can work around the issue by setting context_object_name . 您可以通过设置context_object_name来解决此问题。 For example, this will enable the {{ user_object }} context variable, set to the user of the DetailView: 例如,这将启用{{ user_object }}上下文变量,设置为DetailView的用户:

url(
    r'^users/(?P<pk>\d+)/$',
    DetailView.as_view(
        model = User,
        template_name = 'doors/users/detail.html',
        context_object_name = 'user_object'
    ),
    name = 'users_detail'
)

Dig deeper, read the documentation for get_context_object_name() . 深入挖掘,阅读get_context_object_name()的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM