---
slug: "django-のユニットテストで-request-を作るにはresponse-の-wsgi_request-を取得するのが楽"
title: "To Create a Request in Django Unit Tests, Retrieving the wsgi_request from the Response is Convenient"
description: "When creating unit tests in Django, there are times when you need a Request (WSGIRequest)."
url: "https://www.ytyng.com/en/blog/django-のユニットテストで-request-を作るにはresponse-の-wsgi_request-を取得するのが楽"
publish_date: "2022-03-21T08:57:58Z"
created: "2022-03-21T08:57:58Z"
updated: "2026-02-27T01:22:56.578Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/1d38e7de767a45a3840b0f6597601695.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# To Create a Request in Django Unit Tests, Retrieving the wsgi_request from the Response is Convenient

<p>When creating unit tests in Django, there are times when you need a Request (WSGIRequest).</p>
<p>There is something called <code>django.test.RequestFactory</code>, which allows you to create requests. However, creating requests this way can be quite cumbersome, and since the requests created do not pass through the middleware, it can be challenging to use them for view-level tests.</p>
<p>Instead, using the test client to actually make requests to appropriate views is easier. The response returned contains a request property called <code>wsgi_request</code>, which you can use. These requests have also passed through the middleware.</p>
<pre>client = self.client_class()<br />response = client.post(...)<br />request = response.wsgi_request</pre>
<p></p>
