#25397: DetailView of User model breaks user context variable in templates
-----------------------------------+--------------------
     Reporter:  mathieuhinderyckx  |      Owner:  nobody
         Type:  Bug                |     Status:  new
    Component:  Generic views      |    Version:  1.8
     Severity:  Normal             |   Keywords:
 Triage Stage:  Unreviewed         |  Has patch:  0
Easy pickings:  0                  |      UI/UX:  0
-----------------------------------+--------------------
 == Problem ==
 I have a custom User model, and a DetailView of that Model.

 urls.py:

 {{{
 urlpatterns = [
     url(r'^(?P<pk>\d+)/$', views.UserDetailView.as_view(), name='user-
 detail'),
 ]
 }}}

 views.py:

 {{{
 class UserDetailView(DetailView):
     model = User
     template_name = 'app_example/profile.html'
 }}}

 Template:

 {{{
 <h2>Log in information</h2>
 {% if user.is_authenticated %}
     <p>User authenticated. User oject: {{user}}.</p>
 {% else %}
     <p>User not authenticated. User object: {{user}}.</p>
 {% endif %}
  <h2>DetailView Object</h2>
 <p>{{object}}</p>
 }}}

 To replicate the problem; Create 2 users, log in with 1 user, and visit
 the detailpage with the pk of the 2nd user. Do this also when both users
 are logged out.

 Now, what happens is that the user.is_authenticated block is (wrongfully)
 always shown, and {{user}} is always the Detailview object instead of the
 request.user. The {{object}} is the Detailview object (as expected).

 == Problem ==
 The documentation and example on DetailView mentions that its item is
 available through {{object}}. However, it's also available by default as
 {{obj._meta.model_name}} through the get_object_context_name() method from
 the SingleObjectMixin, which is not clearly mentioned. When using the
 'User' model, this interferes with the {{user}} context variable from
 middleware. In any case (user = request.user or user = object), the
 'is_authenticad' field on that variable should evaluate to False in the
 example above, but does not, why that is, is not clear to me yet. As this
 {{user}} variable is commonly used with authentication on sites, I think
 this should be mentioned clearly. Now, for example menu's for logged-in
 users only are shown while this shouldn't be. If this variable is the
 object from the DetailView instead of the request, this might lead to a
 lot of unexpected behaviour when developing templates.


 == Workaround ==
 set 'context_object_name' on the DetailView to something else.

--
Ticket URL: <https://code.djangoproject.com/ticket/25397>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/060.3b087b2f038077fc41b392890126979b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to