Opened 5 years ago

Closed 5 years ago

#20294 closed Uncategorized (fixed)

Problem with generic views when the model is called 'User'.

Reported by: ttt@… Owned by: Zbigniew Siciarz
Component: Documentation Version: 1.5
Severity: Normal Keywords: user generic views context variable
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



I have extended the Django User model in order to have a customized authentication.
And my model class is also called 'User'.
I am successfully using generic views default configuration for all models except for the 'User'.

According to the documentation, the context variable will be inferred from the model name.
But there seems to be an extra context variable called user, that is always present and provides information about the authenticated user, for which I could not find mention in the documentation of 1.5, but is described in 1.2 docs here:


The following code does not work as expected:

        url(r'^user/(?P<pk>\d+)/$', DetailView.as_view( model = User ), name = 'user'),
  • user_detail.html
    {% if user %}
        <p>id                    : {{                    }}</p>
        <p>name                  : {{                  }}</p>
        <p>description           : {{ user.description           }}</p>
        <p>email                 : {{                 }}</p>

Instead of getting the details for the user with the corresponding pk ( is present in the URL, the authenticated user details are always returned.
There is no warning in the logs about the override, of the user object retrieved from the database, by the authenticated user object within the template context variables namespace.


The desired behavior is obtained after changing the context object name from the default 'user' to 'u', as follows:

        url(r'^user/(?P<pk>\d+)/$', DetailView.as_view( model = User, context_object_name = 'u' ), name = 'user'),
  • user_detail.html
    {% if u %}
        <p>id                    : {{                    }}</p>
        <p>name                  : {{                  }}</p>
        <p>description           : {{ u.description           }}</p>
        <p>email                 : {{                 }}</p>

This way I can obtain the details for any given user, by id.

Change History (5)

comment:1 Changed 5 years ago by anonymous

Keywords: user generic views context variable added

comment:2 Changed 5 years ago by Matt McClanahan

Component: Generic viewsDocumentation
Triage Stage: UnreviewedAccepted

I think it's worth adding a mention in the generic views docs that TemplateResponse uses RequestContext by default and linking to which discusses how processors can overwrite existing context variables.

comment:3 Changed 5 years ago by Zbigniew Siciarz

Owner: changed from nobody to Zbigniew Siciarz
Status: newassigned

comment:4 Changed 5 years ago by Zbigniew Siciarz

comment:5 Changed 5 years ago by Florian Apolloner <florian@…>

Resolution: fixed
Status: assignedclosed

In 0b0741602b18928a418ba4661dc24b880daa5253:

Fixed #20294 -- Documented context processors in TemplateResponseMixin.

Note: See TracTickets for help on using tickets.
Back to Top