使用例子:
以下是request 的源码:
- >>> from django.http import *
- >>> request = HttpRequest()
- >>> request.__doc__
- >>> help(request)
- >>> request.META
- 29: {}
- >>> request.is_ajax()
- 30: False
- >>> request.GET
- 31: {}
- >>> request.FILES
- 32: {}
以下是request 的源码:
- class HttpRequest(object):
- """A basic HTTP request."""
- # The encoding used in GET/POST dicts. None means use default setting.
- _encoding = None
- _upload_handlers = []
- def __init__(self):
- self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
- self.path = ''
- self.path_info = ''
- self.method = None
- def __repr__(self):
- return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
- (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),
- pformat(self.META))
- def get_host(self):
- """Returns the HTTP host using the environment or request headers."""
- # We try three options, in order of decreasing preference.
- if 'HTTP_X_FORWARDED_HOST' in self.META:
- host = self.META['HTTP_X_FORWARDED_HOST']
- elif 'HTTP_HOST' in self.META:
- host = self.META['HTTP_HOST']
- else:
- # Reconstruct the host using the algorithm from PEP 333.
- host = self.META['SERVER_NAME']
- server_port = str(self.META['SERVER_PORT'])
- if server_port != (self.is_secure() and '443' or '80'):
- host = '%s:%s' % (host, server_port)
- return host
- def get_full_path(self):
- return ''
- def build_absolute_uri(self, location=None):
- """
- Builds an absolute URI from the location and the variables available in
- this request. If no location is specified, the absolute URI is built on
- ``request.get_full_path()``.
- """
- if not location:
- location = self.get_full_path()
- if not absolute_http_url_re.match(location):
- current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
- self.get_host(), self.path)
- location = urljoin(current_uri, location)
- return iri_to_uri(location)
- def is_secure(self):
- return os.environ.get("HTTPS") == "on"
- def is_ajax(self):
- return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
- def _set_encoding(self, val):
- """
- Sets the encoding used for GET/POST accesses. If the GET or POST
- dictionary has already been created, it is removed and recreated on the
- next access (so that it is decoded correctly).
- """
- self._encoding = val
- if hasattr(self, '_get'):
- del self._get
- if hasattr(self, '_post'):
- del self._post
- def _get_encoding(self):
- return self._encoding
- encoding = property(_get_encoding, _set_encoding)
- def _initialize_handlers(self):
- self._upload_handlers = [uploadhandler.load_handler(handler, self)
- for handler in settings.FILE_UPLOAD_HANDLERS]
- def _set_upload_handlers(self, upload_handlers):
- if hasattr(self, '_files'):
- raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
- self._upload_handlers = upload_handlers
- def _get_upload_handlers(self):
- if not self._upload_handlers:
- # If thre are no upload handlers defined, initialize them from settings.
- self._initialize_handlers()
- return self._upload_handlers
- upload_handlers = property(_get_upload_handlers, _set_upload_handlers)
- def parse_file_upload(self, META, post_data):
- """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
- self.upload_handlers = ImmutableList(
- self.upload_handlers,
- warning = "You cannot alter upload handlers after the upload has been processed."
- )
- parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
- return parser.parse()