Extra Wrappers

Warning

Deprecated since version 0.15: All classes in this module have been moved or deprecated andwill be removed in version 1.0. Check the docs for the statusof each class.

werkzeug.contrib.wrappers

Extra wrappers or mixins contributed by the community. These wrappers canbe mixed in into request objects to add extra functionality.

Example:

  1. from werkzeug.wrappers import Request as RequestBase
  2. from werkzeug.contrib.wrappers import JSONRequestMixin
  3.  
  4. class Request(RequestBase, JSONRequestMixin):
  5. pass

Afterwards this request object provides the extra functionality of theJSONRequestMixin.

  • class werkzeug.contrib.wrappers.JSONRequestMixin

Deprecated since version 0.15: Moved to werkzeug.wrappers.json.JSONMixin. This oldimport will be removed in version 1.0.

  • class werkzeug.contrib.wrappers.ProtobufRequestMixin
  • Add protobuf parsing method to a request object. This will parse theinput data through protobuf if possible.

BadRequest will be raised if the content-typeis not protobuf or if the data itself cannot be parsed property.

Deprecated since version 0.15: This mixin will be removed in version 1.0.

  • parseprotobuf(_proto_type)
  • Parse the data into an instance of proto_type.

  • protobufcheck_initialization = True_

  • by default the ProtobufRequestMixin will raise aBadRequest if the object is notinitialized. You can bypass that check by setting thisattribute to False.
  • class werkzeug.contrib.wrappers.RoutingArgsRequestMixin
  • This request mixin adds support for the wsgiorg routing argsspecification.

Deprecated since version 0.15: This mixin will be removed in version 1.0.

  • routing_args
  • The positional URL arguments as tuple.

  • routing_vars

  • The keyword URL arguments as dict.
  • class werkzeug.contrib.wrappers.ReverseSlashBehaviorRequestMixin
  • This mixin reverses the trailing slash behavior of script_rootand path. This makes it possible to use urljoin()directly on the paths.

Because it changes the behavior or Request this class has to bemixed in before the actual request class:

  1. class MyRequest(ReverseSlashBehaviorRequestMixin, Request):
  2. pass

This example shows the differences (for an application mounted on/application and the request going to /application/foo/bar):

normal behaviorreverse behavior
script_root/application/application/
path/foo/barfoo/bar

Deprecated since version 0.15: This mixin will be removed in version 1.0.

  • path
  • Requested path as unicode. This works a bit like the regular pathinfo in the WSGI environment but will not include a leading slash.

  • script_root

  • The root path of the script includling a trailing slash.
  • class werkzeug.contrib.wrappers.DynamicCharsetRequestMixin
  • “If this mixin is mixed into a request class it will providea dynamic charset attribute. This means that if the charset istransmitted in the content type headers it’s used from there.

Because it changes the behavior or Request this class hasto be mixed in before the actual request class:

  1. class MyRequest(DynamicCharsetRequestMixin, Request):
  2. pass

By default the request object assumes that the URL charset is thesame as the data charset. If the charset varies on each requestbased on the transmitted data it’s not a good idea to let the URLschange based on that. Most browsers assume either utf-8 or latin1for the URLs if they have troubles figuring out. It’s stronglyrecommended to set the URL charset to utf-8:

  1. class MyRequest(DynamicCharsetRequestMixin, Request):
  2. url_charset = 'utf-8'

Deprecated since version 0.15: This mixin will be removed in version 1.0.

New in version 0.6.

  • charset
  • The charset from the content type.

  • defaultcharset = 'latin1'_

  • the default charset that is assumed if the content type headeris missing or does not contain a charset parameter. The defaultis latin1 which is what HTTP specifies as default charset.You may however want to set this to utf-8 to better supportbrowsers that do not transmit a charset for incoming data.

  • unknowncharset(_charset)

  • Called if a charset was provided but is not supported bythe Python codecs module. By default latin1 is assumed thento not lose any information, you may override this method tochange the behavior.

Parameters:charset – the charset that was not found.Returns:the replacement charset.

  • class werkzeug.contrib.wrappers.DynamicCharsetResponseMixin
  • If this mixin is mixed into a response class it will providea dynamic charset attribute. This means that if the charset islooked up and stored in the Content-Type header and updatesitself automatically. This also means a small performance hit butcan be useful if you’re working with different charsets onresponses.

Because the charset attribute is no a property at class-level, thedefault value is stored in default_charset.

Because it changes the behavior or Response this class hasto be mixed in before the actual response class:

  1. class MyResponse(DynamicCharsetResponseMixin, Response):
  2. pass

Deprecated since version 0.15: This mixin will be removed in version 1.0.

New in version 0.6.

  • charset
  • The charset for the response. It’s stored inside theContent-Type header as a parameter.

  • defaultcharset = 'utf-8'_

  • the default charset.