============================================== リクエストオブジェクトとレスポンスオブジェクト ============================================== :revision-up-to: 17812 (1.4) .. module:: django.http :synopsis: HTTP のリクエストとレスポンスを扱うためのクラスです。 簡単な概要 ========== Django は、システム全体にわたって、リクエストとレスポンスオブジェクトを使っ て処理状態を受け渡します。 あるページへのリクエストを受け取ると、Django は :class:`~django.http.HttpRequest` オブジェクトを生成します。このオブジェク トにはリクエストのメタデータが入っています。次に Django は適切なビューをロー ドして、 :class:`~django.http.HttpRequest` をビュー関数の最初の引数に渡しま す。各ビューは :class:`~django.http.HttpResponse` オブジェクトを返さねばな りません。 このドキュメントでは :class:`~django.http.HttpRequest` および :class:`~django.http.HttpResponse` オブジェクトの API について説明します。 HttpRequest オブジェクト ======================== .. class:: HttpRequest .. _httprequest-attributes: 属性 ---- ``session`` 以外の属性は全て読み出し専用です。 .. attribute:: HttpRequest.body .. versionchanged:: 1.4 .. Before Django 1.4, ``HttpRequest.body`` was named ``HttpRequest.raw_post_data``. Django 1.4 以前では、``HttpRequest.body`` は ``HttpRequest.raw_post_data`` でした. .. The raw HTTP request body as a byte string. This is useful for processing data in different ways than conventional HTML forms: binary images, XML payload etc. For processing conventional form data, use ``HttpRequest.POST``. 生の HTTP リクエストのバイト文字列です。これは従来の HTML フォームと 異なる、バイナリ画像や XML ペイロードのデータを処理するのに便利です。 従来のフォームのデータを処理するためには ``HttpRequest.POST`` を 使って下さい。 .. versionadded:: 1.3 .. You can also read from an HttpRequest using a file-like interface. See :meth:`HttpRequest.read()`. HttpRequest をファイルのようなインターフェースで読むことも出来ます。 後述の :meth:`HttpRequest.read()` も参照して下さい。 .. attribute:: HttpRequest.path リクエストしているページのフルパスを表す、ドメインを含まない文字列です。 例: ``"/music/bands/the_beatles/"`` .. attribute:: HttpRequest.path_info .. Under some Web server configurations, the portion of the URL after the host name is split up into a script prefix portion and a path info portion (this happens, for example, when using the ``django.root`` option with the :doc:`modpython handler from Apache `). The ``path_info`` attribute always contains the path info portion of the path, no matter what Web server is being used. Using this instead of attr:`~HttpRequest.path` can make your code much easier to move between test and deployment servers. いくつかの Web サーバーの設定において、ホストネーム以降の URL の一部が スクリプトプレフィックスの部分と PATH_INFO の部分に分割されます。 ( 例えば、これが発生するのは、 ``django.root`` オプションを :doc:`Apache と mod_python で Django を動かす ` で使っている場合 ) ``path_info`` 属性には、どの Web サーバーが使われている かに関わらず、常にパスの PATH_INFO の部分が入ります。 :attr:`~HttpRequest.path` の代わりに使うことで、テストサーバーから本番 サーバーへの移行が簡単なコードにすることが出来ます。 .. For example, if the ``WSGIScriptAlias`` for your application is set to ``"/minfo"``, then ``path`` might be ``"/minfo/music/bands/the_beatles/"`` and ``path_info`` would be ``"/music/bands/the_beatles/"``. 例えば、もしアプリケーションの ``WSGIScriptAlias`` が ``"/minfo"`` に 設定されていた場合、 ``path`` は ``"/minfo/music/bands/the_beatles/"`` に ``path_info`` は ``"/music/bands/the_beatles/"`` になるでしょう。 .. attribute:: HttpRequest.method リクエストに使われた HTTP メソッドを表す文字列です。必ず大文字になります。 例:: if request.method == 'GET': do_something() elif request.method == 'POST': do_something_else() .. attribute:: HttpRequest.encoding 提出されたフォームデータのデコードに使われる、現在のエンコーディングを 表す文字列です (``None`` の場合もありますが、この場合は :setting:`DEFAULT_CHARSET` を使います)。この属性を変更すれば、フォーム データにアクセスする際に使われるエンコーディングを指定できます。一度エ ンコーディングを変更すると、変更後に (``GET`` や ``POST`` の) 属性への アクセスはすべて新しい ``encoding`` の値に従って行われます。フォームデー タが :setting:`DEFAULT_CHARSET` 以外の特定のエンコーディングと分かって いる場合に便利です。 .. attribute:: HttpRequest.GET 全ての HTTP GET パラメタが入った辞書ライクなオブジェクトです。後述の :class:`QueryDict` も参照してください。 .. attribute:: HttpRequest.POST 全ての HTTP POST パラメタが入った辞書ライクなオブジェクトです。後述の :class:`QueryDict` も参照してください。 フォームを POST HTTP メソッドで要求し、その際に何らフォームデータを伴わ ないような場合には、リクエストが POST で送られていながらも ``POST`` 辞 書が空になることがあります。従って、リクエストが POST メソッドであるか どうかを調べるために ``if request.POST`` を使うべきではありません。代わ りに ``if request.method == "POST"`` を使ってください (上参照)。 ``POST`` にはファイルアップロードに関する情報は *入っていない* ので注意 してください。 ``FILES`` を参照してください。 .. attribute:: HttpRequest.REQUEST 便宜的な辞書オブジェクトで、 ``POST`` パラメタをまず検索してから、次に ``GET`` パラメタを検索します。 PHP の ``$_REQUEST`` にインスパイアされ た機能です。 例えば、 ``GET = {"name": "john"}`` で ``POST = {"age": '34'}`` の場合、 ``REQUEST["name"]`` は ``"john"`` になり、 ``REQUEST["age"]`` は ``"34"`` になります。 通常は ``GET`` および ``POST`` を使うように強く勧めます。その方が明示的 だからです。 .. attribute:: HttpRequest.COOKIES 全てのクッキーが入った標準の Python 辞書オブジェクトです。キーと値は文 字列です。 .. attribute:: HttpRequest.FILES アップロードされた全てのファイルが入っている辞書ライクオブジェクトです。 ``FILES`` の各キーは ```` の ``name`` に対応しています。 ``FILES`` の各値は後述の :class:`UploadedFile` オブジ ェクトです. 詳しくは :doc:`/topics/files` を参照してください。 ``FILES`` にデータが入るのは、リクエストが ``POST`` であり、かつリクエ ストをポストした ``