... ス にドキュメントされているので参照してください。 自分でミドルウェアを書く ミドルウェアファクトリは、ミドルウェアを返す get_response を取る呼び出し可能なオブジェクトです。ミドルウェアは (ビューと同じように) リクエストを受け取ってレスポンスを返す呼び出し可 ... 能なオブジェクトです。 ミドルウェアは、以下のように関数として書くことができます: def simple_middleware ( get_response ): # One-time configuration and initialization. def middleware ( request ): # Code to be executed for each request before ... # the view (and later middleware) are called. response = get_response ( request ) # Code to be executed for each request/response after # the view is called. return response ret ... ... owed() setup() TemplateView class TemplateView 属性 (とオプションのアクセサ): content_type extra_context http_method_names response_class [ render_to_response() ] template_engine template_name [ get_template_names() ] メソッド as_view() dispatch() get() ... get_context_data() head() http_method_not_allowed() render_to_response() setup() RedirectView class RedirectView 属性 (とオプションのアクセサ): http_method_names pattern_name permanent query_st ... t_context_object_name() ] extra_context http_method_names model pk_url_kwarg query_pk_and_slug queryset [ get_queryset() ] response_class [ render_to_response() ] slug_field [ get_slug_field() ] slug_url_kwarg template_engine template_name [ get_template ... ... ient からインスタンスを作り、次のようにウェブページを取得します。 >>> from django.test import Client >>> c = Client () >>> response = c . post ( "/login/" , { "username" : "john" , "password" : "smith" }) >>> response . status_code 200 >>> response = c . ... get ( "/customer/details/" ) >>> response . content b'<!DOCTYPE html...' この例が示唆しているように、 Client のインスタンスは、Python のインタラクティブなインタプリ ... e = False , * , headers = None , query_params = None , ** extra ) [ソース] 与えられた path に対して GET リクエストを作り、 Response オブジェクトを返します。 Response オブジェクトについては、下のセクションにドキュメントされています。 query_params に渡され ... ... ます; POST ではテンプレートをレンダリングするだけで、 GET のときはまったく異なる処理がしたいときなどです。この場合、 TemplateResponse を直接使えますが、コードが重複する結果となってしまいます。 この理由から、Django は個別の機能を提供する多くの mixin を用意し ... ています。たとえば、テンプレートのレンダリングは TemplateResponseMixin でカプセル化されています。Django のリファレンスドキュメントには すべてのミックスインの完全なドキュメント があります。 ... ixin) が用意されており、クラスベースビュー内のテンプレートを扱うインターフェースに一貫性を保ちやすくなっています。 TemplateResponseMixin Every built in view which returns a TemplateResponse will call the render_to_response() method that TemplateResponseM ... ... てください。より詳しい情報については、 テンプレートコンテキストのレンダリング に関するドキュメントを読んでください。 TemplateResponseMixin class django.views.generic.base. TemplateResponseMixin 適切なコンテキストを指定して TemplateResponse を構築するメカニ ... 例外が発生します。 template_engine テンプレートを読み込む際に使用するテンプレートエンジンの NAME です。 template_engine は response_class の using キーワード引数として渡されます。デフォルトは None で、 Django は設定された全てのエンジンでテンプレートを検索 ... します。 response_class render_to_response メソッドが返す response クラス。 デフォルトは TemplateResponse です。 TemplateResponse インスタン ... ... ンプレートのレンダラーをインストールします。 これによって、今までは調査できなかった、レスポンス上のいくつかの属性(たとえば response.context )を調査できるようになります。 注意点として、このメソッドはテスト用データベースを作成しないので、これに続く命令は既 ... stance of the client for our use >>> client = Client () さて、これでクライアントに仕事を頼む準備ができました。 >>> # get a response from '/' >>> response = client . get ( "/" ) Not Found: / >>> # we should expect a 404 from that address; if you instead s ... ee an >>> # "Invalid HTTP_HOST header" error and a 400 response, you probably >>> # omitted the setup_test_environment() call described earlier. >>> response . status_code 404 >>> # on t ... ... あります。たとえば、使用するテンプレートを変更したり、追加データをコンテキストに追加したりしたくなるかもしれません。 TemplateResponse はそのための方法を提供します。基本的な HttpResponse オブジェクトとは異なり、 TemplateResponse オブジェクトはレスポンスを計 ... ンテキストの詳細を保持します。レスポンスの最終的な出力は、レスポンス処理の後半で必要になるまで計算されません。 SimpleTemplateResponse オブジェクト class SimpleTemplateResponse [ソース] 属性 SimpleTemplateResponse. template_name The name of the template to ... get_template() ), the name of a template, or a list of template names. 例: ['foo.html', 'path/to/bar.html'] SimpleTemplateResponse. context_data テンプレートをレンダリングするときに使用するコンテキストデータ。 これは dict でなければなりません。 例: {'fo ... ... します。それから Django は HttpRequest をビュー関数の最初の関数として渡し、適切なビューを読み込みます。あらゆるビューは HttpResponse オブジェクトを返す必要があります。 このドキュメントでは、 HttpRequest と HttpResponse オブジェクトの API を説明しています。 ... _FOR_FIELDS = [ "HTTP_X_FORWARDED_FOR" , "HTTP_X_FORWARDED_HOST" , "HTTP_X_FORWARDED_SERVER" , ] def __init__ ( self , get_response ): self . get_response = get_response def __call__ ( self , request ): """ Rewrites the proxy headers so that only the mos ... ]: parts = request . META [ field ] . split ( "," ) request . META [ field ] = parts [ - 1 ] . strip () return self . get_response ( request ) This middleware should be positioned before any other middleware that relies on the value of get_host() -- for ... ... /" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. form . send_email () return super () . form_valid ( form ) メモ: FormView は TemplateResponseMixin を継承するため、ここで ... r/<int:pk>/delete/" , AuthorDeleteView . as_view (), name = "author-delete" ), ] 注釈 これらのビューは SingleObjectTemplateResponseMixin を継承しており、 template_name_suffix を使ってモデルに基づいて template_name を構成します。 この例では: CreateView と ... では、API ベースのワークフローと「通常の」フォーム POST で動作するフォームの実装例を紹介します: from django.http import JsonResponse from django.views.generic.edit import CreateView from myapp.models import Author class JsonableResponseMixin : """ Mixin t ... ... 記述します。 シンプルなビュー 以下は、HTML ドキュメントとして、現在の日付と時刻を返すビューです: from django.http import HttpResponse import datetime def current_datetime ( request ): now = datetime . datetime . now () html = '<html lang="en"><body>It is n ... ow %s .</body></html>' % now return HttpResponse ( html ) 1 行ずつコードを見ていきましょう: 最初に、Python の datetime ライブラリ とともに、クラス HttpResponse を django.h ... 機能を的確に表しているので、ビュー関数を current_datetime と呼ぶことにします。 このビューは、生成されたレスポンスを含む HttpResponse のオブジェクトを返します。それぞれのビュー関数には、 HttpResponse オブジェクトを返す義務があります。(例外もありますので、後 ... 2026年10月08日