[Django] #25397: DetailView of User model breaks user context variable in templates

54 views
Skip to first unread message

Django

unread,
Sep 13, 2015, 11:41:52 AM9/13/15
to django-...@googlegroups.com
#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.

Django

unread,
Sep 13, 2015, 1:06:55 PM9/13/15
to django-...@googlegroups.com
#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 | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by mlorant):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Your solution seems to break forward compatibility, since the ''public''
method `get_context_object_name` describes the default behavior in the
current documentation: [https://docs.djangoproject.com/en/1.8/ref/class-
based-views/mixins-single-
object/#django.views.generic.detail.SingleObjectMixin.get_context_object_name],
plus I think it is a good implementation, besides this case...

Maybe we could either :
- exclude some reserved variable names (especially user and site).
Problem: not really pretty and would be reserved to variables included by
Django ;
- change the behaviour if the variable is already used by one of the
installed middleware. Problem: if you remove a middleware, it might break
the generic view template (since the variable could be renamed as a side
effect).

Both solutions have limits, especially the second one...

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:1>

Django

unread,
Sep 14, 2015, 1:29:24 PM9/14/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context varaibles and template context
processor varaibles
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* type: Bug => Cleanup/optimization
* component: Generic views => Documentation
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

I think trying to be "smart" and change or disallow the certain context
variable names won't be worth it. I'd rather work to deprecate the auth
context_processor (`user` can be replaced by `request.user`, but a
solution for replacing `perms` isn't so clear). Meanwhile, we can
certainly improve the documentation.

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:2>

Django

unread,
Sep 14, 2015, 1:34:56 PM9/14/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context variables and template context
processor variables
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:3>

Django

unread,
Oct 20, 2015, 3:59:10 PM10/20/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context variables and template context
processor variables
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: jbzdak
Type: Cleanup/optimization | Status: assigned

Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by jbzdak):

* status: new => assigned
* owner: nobody => jbzdak


Comment:

I'll do it

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:4>

Django

unread,
Oct 21, 2015, 7:00:08 PM10/21/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context variables and template context
processor variables
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: jbzdak
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/5456 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:5>

Django

unread,
Oct 23, 2015, 3:24:12 PM10/23/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context variables and template context
processor variables
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: jbzdak
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"494b7986a3e5996d857b085f188a630d1504d9ca" 494b7986]:
{{{
#!CommitTicketReference repository=""
revision="494b7986a3e5996d857b085f188a630d1504d9ca"
Fixed #25397 -- Documented class-based view context variable clash with
context processors.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:6>

Django

unread,
Oct 23, 2015, 3:24:33 PM10/23/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context variables and template context
processor variables
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: jbzdak
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"488538e55e8f32c00f43f66367a63a4e6a56e1c6" 488538e]:
{{{
#!CommitTicketReference repository=""
revision="488538e55e8f32c00f43f66367a63a4e6a56e1c6"
[1.9.x] Fixed #25397 -- Documented class-based view context variable clash
with context processors.

Backport of 494b7986a3e5996d857b085f188a630d1504d9ca from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:8>

Django

unread,
Oct 23, 2015, 3:24:33 PM10/23/15
to django-...@googlegroups.com
#25397: Warn of clash between generic view context variables and template context
processor variables
--------------------------------------+------------------------------------
Reporter: mathieuhinderyckx | Owner: jbzdak
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"d83454fbf2eb677fa54bcb5f7ab10d5007b6a147" d83454fb]:
{{{
#!CommitTicketReference repository=""
revision="d83454fbf2eb677fa54bcb5f7ab10d5007b6a147"
[1.8.x] Fixed #25397 -- Documented class-based view context variable clash
with context processors.

Backport of 494b7986a3e5996d857b085f188a630d1504d9ca from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25397#comment:7>

Reply all
Reply to author
Forward
0 new messages