Day19はdjango/response.pyのclass HttpResponseBase
class HttpResponseBase: """ An HTTP response base class with dictionary-accessed headers. This class doesn't handle content. It should not be used directly. Use the HttpResponse and StreamingHttpResponse subclasses instead. """ status_code = 200
・status_codeはhttpにおける状態のこと、200 OK。
def __init__(self, content_type=None, status=None, reason=None, charset=None, headers=None): self.headers = ResponseHeaders(headers or {}) self._charset = charset
・クラスにおけるインスタンスそのものについて書かれている。ResponseHeadersはこのクラスに書かれている上にあるクラスを用いている(この記事では表示していない)。その中に書かれているheadersか{}(辞書)を投入する。charsetはそのまま。
if content_type and 'Content-Type' in self.headers: raise ValueError( "'headers' must not contain 'Content-Type' when the " "'content_type' parameter is provided." )
・content_typeと'Content-Type'が入っていれば、ValueErrorを表示する。中身は"--"
if 'Content-Type' not in self.headers: if content_type is None: content_type = 'text/html; charset=%s' % self.charset self.headers['Content-Type'] = content_type self._resource_closers = [] # This parameter is set by the handler. It's necessary to preserve the # historical behavior of request_finished. self._handler_class = None self.cookies = SimpleCookie() self.closed = False
・'Content-Type' がインスタンスのheadersになかったら、
content-typeがNoneだったら、content-typeにtext/htmlとcharsetを入れ、
それをheaderインスタンスの'Content-Type'にcontent_typeを入れる
self._resource_closersという空のリストを用意
self._handler_class = None
self.cookies = SimpleCookie()
self.closed = False
の各インスタンス自身を作成
if status is not None: try: self.status_code = int(status) except (ValueError, TypeError): raise TypeError('HTTP status code must be an integer.')
もしstatusがなかったら
例外処理でself.status_codeに整数statusを代入
できなかったらTypeErrorを表示
if not 100 <= self.status_code <= 599: raise ValueError('HTTP status code must be an integer from 100 to 599.') self._reason_phrase = reason
・status_codeが100以上599ではなかったら、ValueErrorを表示
・self._reason_phraseインスタンスを用意する
以上がHttpResponseBaseの読み。これがHttpResponseに使用される。明日はHttpResponseを読む。
class HttpResponseBase: """ An HTTP response base class with dictionary-accessed headers. This class doesn't handle content. It should not be used directly. Use the HttpResponse and StreamingHttpResponse subclasses instead. """ status_code = 200 def __init__(self, content_type=None, status=None, reason=None, charset=None, headers=None): self.headers = ResponseHeaders(headers or {}) self._charset = charset if content_type and 'Content-Type' in self.headers: raise ValueError( "'headers' must not contain 'Content-Type' when the " "'content_type' parameter is provided." ) if 'Content-Type' not in self.headers: if content_type is None: content_type = 'text/html; charset=%s' % self.charset self.headers['Content-Type'] = content_type self._resource_closers = [] # This parameter is set by the handler. It's necessary to preserve the # historical behavior of request_finished. self._handler_class = None self.cookies = SimpleCookie() self.closed = False if status is not None: try: self.status_code = int(status) except (ValueError, TypeError): raise TypeError('HTTP status code must be an integer.') if not 100 <= self.status_code <= 599: raise ValueError('HTTP status code must be an integer from 100 to 599.') self._reason_phrase = reason