简体   繁体   English

Django的get_context_data函数不起作用?

[英]Django's get_context_data function not working?

I'm practicing with Django's Class Based View. 我正在使用Django的基于类的视图进行练习。

It seems like my overridden get_context_data function is not working properly, but I have no idea what is wrong :( 似乎我重写的get_context_data函数无法正常工作,但我不知道出什么问题了:(

My code: 我的代码:

urls.py urls.py

url(r'^user/(?P<pk>\d+)/posts/$', UserPosts.as_view(), name='user_post'),

views.py views.py

class UserPosts(ListView):
    template_name = 'app_blog/user_posts_page.html'
    context_object_name = 'post_list'

    def get_queryset(self):
        self.user = get_object_or_404(User, id=self.kwargs['pk'])
        return self.user.post_set.order_by('-id')
    def get_context_data(self, **kwargs):   
        context = super(UserPosts, self).get_context_data(**kwargs)
        context['user'] = self.user
        return context

user_post_page.html user_post_page.html

{% block content %}
    <div class="main_content">
        <h2>Welcome to {{user.username}}'s User Post Page!</he>
        <ul>
        {% for post in post_list %}
            <li><a href="/blog/post/{{post.id}}/">{{post.post_title}}</a></li>
        {% endfor %}
        </ul>
    </div>
{% endblock %}

The html page correctly displays the user's post_list, BUT the h2 tag displays: html页面正确显示用户的post_list,但是h2标签显示:

Welcome to 's User Post Page!

I'm pretty sure I passed the 'user' variable in the get_context_data function, but the html page does not displa the user.username... Any idea why this is happening :(?? 我很确定我在get_context_data函数中传递了'user'变量,但是html页面没有显示user.username ...为什么会这样:( ??

Thanks 谢谢

Use another name that is not user . 使用不是user其他名称。 It seems like RequestContext overwrite user variable. 似乎RequestContext覆盖了user变量。

Please see the default TEMPLATE_CONTEXT_PROCESSORS , which set the django.contrib.auth.context_processors.auth . 请参阅默认的TEMPLATE_CONTEXT_PROCESSORS ,它设置了django.contrib.auth.context_processors.auth

If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain these variables: 如果TEMPLATE_CONTEXT_PROCESSORS包含此处理器,则每个RequestContext将包含以下变量:

  • user – An auth.User instance representing the currently logged-in user (or an AnonymousUser instance, if the client isn't logged in). user –代表当前登录userauth.User实例(如果未登录客户端,则为AnonymousUser实例)。
  • perms – An instance of django.contrib.auth.context_processors.PermWrapper , representing the permissions that the currently logged-in user has. permsdjango.contrib.auth.context_processors.PermWrapper的实例,代表当前登录用户所拥有的权限。

So you'd better give your user variable another name. 因此,最好给您的user变量另一个名称。

As the other answers said, don't use the variable name user . 就像其他答案所说的那样,不要使用变量名user But even more importantly, in this particular case the ListView is not the best generic class to use; 但更重要的是,在这种特殊情况下, ListView不是最好的通用类。 instead, you're better off using the DetailView , which makes your view much simpler: 相反,最好使用DetailView ,它使您的视图更简单:

class UserPosts(DetailView):
    model = User
    template_name = 'app_blog/user_posts_page.html'
    context_object_name = 'listed_user'

The only change in your template is to use listed_user instead of the user variable. 模板中的唯一更改是使用listed_user而不是user变量。 Since DetailView already looks into the pk URL argument, it will select and return the right User automatically. 由于DetailView已经调查了pk URL参数,它将自动选择并返回合适的User

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

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