[Django] #36909: Avoid using context.request directly

2 views
Skip to first unread message

Django

unread,
Feb 7, 2026, 6:44:41 PM (2 days ago) Feb 7
to django-...@googlegroups.com
#36909: Avoid using context.request directly
-----------------------------+-------------------------------------------
Reporter: Jake Howard | Type: Bug
Status: new | Component: Template system
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+-------------------------------------------
The `RequestContext` subclass sets `context.request` to the current
request object. This attribute isn't available when `Context` is used, or
when something context-looking is used instead. For example, the new
`querystring` tag accesses `context.request`, making it incompatible in
some cases.

I'd suggest that uses of `context.request` directly be replaced with
`context["request"]`, and that `RequestContext` automatically add
`request` to its own context if it's not there already. It might also be
worth renaming the attribute to `_request` to hint it shouldn't be used
externally.

This likely only occurs when template context is being incorrectly
handled, but it's likely worth improving regardless.
--
Ticket URL: <https://code.djangoproject.com/ticket/36909>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 7, 2026, 8:22:55 PM (2 days ago) Feb 7
to django-...@googlegroups.com
#36909: Avoid using context.request directly
---------------------------------+--------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: Bug | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by jaffar Khan):

* cc: jaffar Khan (added)

Comment:

Like to work on it if accepted.
--
Ticket URL: <https://code.djangoproject.com/ticket/36909#comment:1>

Django

unread,
Feb 8, 2026, 2:39:12 AM (2 days ago) Feb 8
to django-...@googlegroups.com
#36909: Avoid using context.request directly
---------------------------------+--------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: Bug | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Vidhi Singh):

This seems like a reasonable cleanup to avoid relying on context.request,
which isn’t always present.
I’m happy to work on a patch if maintainers agree with the proposed
direction.
--
Ticket URL: <https://code.djangoproject.com/ticket/36909#comment:2>

Django

unread,
Feb 8, 2026, 2:44:44 AM (2 days ago) Feb 8
to django-...@googlegroups.com
#36909: Avoid using context.request directly
---------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: jaffar Khan
Type: Bug | Status: assigned
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by jaffar Khan):

* owner: (none) => jaffar Khan
* status: new => assigned

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

Django

unread,
Feb 9, 2026, 10:54:49 AM (18 hours ago) Feb 9
to django-...@googlegroups.com
#36909: Avoid using context.request directly
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: jaffar
| Khan
Type: Bug | Status: closed
Component: Template system | Version: 5.1
Severity: Normal | Resolution: needsinfo
Keywords: querystring | Triage Stage:
requestcontext | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* easy: 1 => 0
* keywords: => querystring requestcontext
* resolution: => needsinfo
* status: assigned => closed

Comment:

Hey Jake, thank you for the ticket. I think I have suffered from this
issue when using `querystring` in a base template that is later used for a
`500.html` template, but I would like to understand your motivation/use
case (or failing test case?) before accepting the ticket. My main concern
is the backward incompatibility (and I have also removed the easy picking
flag because of that).
--
Ticket URL: <https://code.djangoproject.com/ticket/36909#comment:4>

Django

unread,
Feb 9, 2026, 4:53:45 PM (12 hours ago) Feb 9
to django-...@googlegroups.com
#36909: Avoid using context.request directly
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: jaffar
| Khan
Type: Bug | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: querystring | Triage Stage:
requestcontext | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jake Howard):

* resolution: needsinfo =>
* status: closed => new

Comment:

My main motivation is exactly that - that there are situations where the
context in a template is a `Context` instance rather than `RequestContext`
(unintentionally or otherwise). I think the primary time this comes up is
when a template is rendered manually, rather than using Django's helpers.
[https://github.com/wagtail/wagtail/issues/12662 Wagtail] has hit this
issue in its StreamField, which renders templates directly, rather than as
part of the final request rendering (ie using `Context` rather than the
special `RequestContext`).

Having `querystring` (and other future tags) depend on having a `request`
in the context makes sense, but assuming it is a `RequestContext` instance
feels unnecessarily specific, especially since `RequestContext` is
undocumented.

Backwards compatibility wise, since `RequestContext` isn't a public API,
tweaking the `RequestContext.request` behaviour shouldn't be especially
breaking. Similarly, users of `querystring` won't notice a difference, and
if anything will see strange incompatibilities resolved.
--
Ticket URL: <https://code.djangoproject.com/ticket/36909#comment:5>
Reply all
Reply to author
Forward
0 new messages