Opened 2 years ago

Closed 8 months ago

#18356 closed Bug (fixed)

Test Client response does not contain Context with nested requests

Reported by: bmihelac Owned by: nobody
Component: Testing framework Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


get() and post() methods returns Response object, including context variable that was used to render the template that produced the response content.

However, if view calls another view before returning response, processing nested view will disconnect template_rendered signal returning no Context object.

I am attaching failing test case.

Attachments (1)

18356-test-client-context-lost-for-nested-views.diff (3.4 KB) - added by bmihelac 2 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 2 years ago by bmihelac

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by bmihelac

Fix for this can be in giving dispatch_uid unique value for each request. This is added to branch and pull request if there are no objections.

comment:3 Changed 22 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Design decision needed
  • Type changed from Uncategorized to Bug

Well, the test client wasn't designed to be used from within a view.

Could you show us the patch that would fix this problem?

comment:4 follow-up: Changed 22 months ago by anonymous

Changeset that fixes this issue is:

(branch has test case and fix)

BTW, i found this issue while trying to fix django-test-utils, which has middleware that records context for every request made. Testing that necessarily leads to nesting requests in a test case.

comment:5 in reply to: ↑ 4 Changed 22 months ago by bmihelac

Accidentally posted this as anonymous.

comment:6 Changed 13 months ago by aaugustin

  • Triage Stage changed from Design decision needed to Accepted

This is quite an edge case, but I can't see any downside to the fix.

comment:7 Changed 8 months ago by Tim Graham <timograham@…>

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

In 0cac4fbf699bb6a3de5f4a48c6e047a4dc6c2df7:

Fixed #18356 -- Gave the test client signals.template_rendered call a unique dispatch_uid

This prevents the test client context from being lost when the client
is used in a nested fashion.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.