Opened 3 years ago

Closed 3 years ago

#20294 closed Uncategorized (fixed)

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

Reported by: ttt@… Owned by: zsiciarz
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 3 years ago by anonymous

  • Keywords user generic views context variable added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by mattmcc

  • Component changed from Generic views to Documentation
  • Triage Stage changed from Unreviewed to Accepted

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 3 years ago by zsiciarz

  • Owner changed from nobody to zsiciarz
  • Status changed from new to assigned

comment:4 Changed 3 years ago by zsiciarz

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

  • Resolution set to fixed
  • Status changed from assigned to closed

In 0b0741602b18928a418ba4661dc24b880daa5253:

Fixed #20294 -- Documented context processors in TemplateResponseMixin.

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