... 生成します。それから Django は HttpRequest をビュー関数の最初の関数として渡し、適切なビューを読み込みます。あらゆるビューは HttpResponse オブジェクトを返す必要があります。 このドキュメントでは、 HttpRequest と HttpResponse オブジェクトの API を説明しています。 ... 。たとえば: >>> q = QueryDict ( mutable = True ) >>> q [ "next" ] = "/a&b/" >>> q . urlencode ( safe = "/" ) 'next=/a%26b/' HttpResponse オブジェクト class HttpResponse [ソース] Django によって自動的に生成される HttpRequest オブジェクトとは対照的に、 HttpResp ... onse オブジェクトの作成はあなたの責任です。プログラム内に記述されるあらゆるビューは、 HttpResponse をインスタンス化し、データを格納して返す必要があります。 HttpResponse クラスは django.http モジュール内に存在します。 使い ... ... ーを記述します。 シンプルなビュー 以下は、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 オブジェクトを返す義務があります。(例外もありますので、後 ... ... のライブラリを使うコツは、 csv モジュールの CSV 生成機能がファイルライクオブジェクトに対して働くことを利用します。Django の HttpResponse オブジェクトもまた、ファイルライクオブジェクトなのです。 以下に例を示します: import csv from django.http import HttpRespon ... se def some_view ( request ): # Create the HttpResponse object with the appropriate CSV header. response = HttpResponse ( content_type = "text/csv" , headers = { "Content-Disposi ... には、 csv.writer の最初の引数として response を渡すだけです。 csv.writer 関数はファイルライクなオブジェクトを要求しますが、 HttpResponse オブジェクトはこの要求に適合しています。 CSV ファイルの行ごとに writer.writerow を呼び、 iterable を渡します。 CSV モジュー ... ... に追加していきましょう。これから追加するviewでは引数をとります。 polls/views.py def detail ( request , question_id ): return HttpResponse ( "You're looking at question %s ." % question_id ) def results ( request , question_id ): response = "You're looking at t ... he results of question %s ." return HttpResponse ( response % question_id ) def vote ( request , question_id ): return HttpResponse ( "You're voting on question %s ." % qu ... 名を区切ります。 実際に動作するビューを書く 各ビューには二つの役割があります: 一つはリクエストされたページのコンテンツを含む HttpResponse オブジェクトを返すこと、もう一つは Http404 のような例外の送出です。それ以外の処理はユーザ次第です。 ビューはデータベースか ... ... ルウェアは、 startproject が生成する設定ファイルでは最初から有効化されています。 デフォルトでは、このミドルウェアはすべての HttpResponse の X-Frame-Options ヘッダを DENY に設定します。もし他の値にしたい場合は X_FRAME_OPTIONS を次のように設定します: X_FRAME_OP ... しれません。そのような場合にはヘッダを設定しないよう、ビューデコレータでミドルウェアに指示できます: from django.http import HttpResponse from django.views.decorators.clickjacking import xframe_options_exempt @xframe_options_exempt def ok_to_load_in_a_frame ( ... request ): return HttpResponse ( "This page is safe to load in a frame on any site." ) 注釈 フレームやiframe内でフォームを送信したり、セッションクッキーに ... ... will be added to the response returned by that view, effectively disabling CSP for that view. 例: from django.http import HttpResponse from django.utils.csp import CSP from django.views.decorators.csp import csp_override @csp_override ( { "default-src" : [ ... CSP . SELF ], "img-src" : [ CSP . SELF , "data:" ], } ) def my_view ( request ): return HttpResponse ( "Custom Content-Security-Policy header applied" ) @csp_override ({}) def my_other_view ( request ): return HttpResponse ... ed to the response returned by that view, effectively disabling report-only CSP for that view. 例: from django.http import HttpResponse from django.utils.csp import CSP from django.views.decorators.csp import csp_report_only_override @csp_report_only_overrid ... ... ーの関係と歴史的経緯 まず初めに存在したのは、ビュー関数の規約だけでした。Django は、定義された関数に HttpRequest を渡して、 HttpResponse が返ってくることを期待していました。Django が提供する機能はこの範囲まででした。 早い内に、ビューの開発には共通のイディオム ... すみます。 そのため、ビュー関数の場合に HTTP GET をハンドリングするコードが次のようになるとすると、 from django.http import HttpResponse def my_view ( request ): if request . method == "GET" : # <view logic> return HttpResponse ( "result" ) クラスベースのビュ ... ーでは以下のようになります。 from django.http import HttpResponse from django.views import View class MyView ( View ): def get ( self , request ): # <view logic> return HttpResponse ( "res ... ... ns__() (backends.base.SessionBase のメソッド) (QueryDict のメソッド) __delitem__() (backends.base.SessionBase のメソッド) (HttpResponse のメソッド) __eq__() (Model のメソッド) __getattr__() (Area のメソッド) (Distance のメソッド) __getitem__() (backends.base ... .SessionBase のメソッド) (HttpResponse のメソッド) (OGRGeometry のメソッド) (QueryDict のメソッド) (SpatialReference のメソッド) __hash__() (Model のメソッド) __ ... init__() (HttpResponse のメソッド) (QueryDict のメソッド) (requests.RequestSite のメソッド) (SimpleTemplateResponse のメソッド) (SyndicationFeed ... ... = None , using = None ) [ソース] 与えられたテンプレートとコンテキスト辞書を組み合わせてレンダリングされたテキストを持つ、 HttpResponse オブジェクトを返します。 Django does not provide a shortcut function which returns a TemplateResponse because the construc ... " , { "foo" : "bar" , }, content_type = "application/xhtml+xml" , ) この例は次のコードと等価です。 from django.http import HttpResponse from django.template import loader def my_view ( request ): # View code here... t = loader . get_template ( "myapp/index.h ... tml" ) c = { "foo" : "bar" } return HttpResponse ( t . render ( c , request ), content_type = "application/xhtml+xml" ) redirect() redirect ( to , * args , permanent = Fal ... ... text_lazy() この例では、 "Welcome to my site." というテキストが翻訳文字列としてマークされています: from django.http import HttpResponse from django.utils.translation import gettext as _ def my_view ( request ): output = _ ( "Welcome to my site." ) return Htt ... e ( output ) 別名を付けなくても同じコードを書くことができます。次の例は、上の例とまったく同一です: from django.http import HttpResponse from django.utils.translation import gettext def my_view ( request ): output = gettext ( "Welcome to my site." ) return Ht ... く同一です: def my_view ( request ): words = [ "Welcome" , "to" , "my" , "site." ] output = _ ( " " . join ( words )) return HttpResponse ( output ) 翻訳は変数にも適用されます。次の例もまた、上の例とまったく同一です: def my_view ( request ): sentence = "Welcom ... 2026年10月08日