paulb@335 | 1 | <?xml version="1.0" encoding="iso-8859-1"?> |
paulb@335 | 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
paulb@335 | 3 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@335 | 4 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paulb@335 | 5 | <head> |
paulb@335 | 6 | <title>Character Encodings</title> |
paulb@335 | 7 | <meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" /> |
paulb@335 | 8 | <link href="styles.css" rel="stylesheet" type="text/css" /> |
paulb@335 | 9 | </head> |
paulb@335 | 10 | |
paulb@335 | 11 | <body> |
paulb@335 | 12 | <h1>Character Encodings</h1> |
paulb@335 | 13 | |
paulb@335 | 14 | <p>WebStack tries to let applications work with Unicode as much as possible, |
paulb@335 | 15 | but there are two places where plain Python strings can be involved:</p> |
paulb@335 | 16 | <ul> |
paulb@335 | 17 | <li>When <a href="responses.html">output is prepared</a> - for example, Web |
paulb@335 | 18 | pages.</li> |
paulb@335 | 19 | <li>When <a href="parameters.html">inspecting request parameters</a>.</li> |
paulb@335 | 20 | </ul> |
paulb@335 | 21 | |
paulb@335 | 22 | <h2>Recommendations</h2> |
paulb@335 | 23 | |
paulb@335 | 24 | <p>Although WebStack has some support for detecting character encodings used |
paulb@335 | 25 | in requests, it is often best for your application to exercise control over |
paulb@335 | 26 | which encoding is used when <a href="parameters.html">inspecting request |
paulb@335 | 27 | parameters</a> and when <a href="responses.html">producing responses</a>. The |
paulb@335 | 28 | best way to do this is to decide which encoding is most suitable for the data |
paulb@335 | 29 | presented and received in your application and then to use it throughout. |
paulb@335 | 30 | Here is an outline of code which does this:</p> |
paulb@335 | 31 | <pre>from WebStack.Generic import ContentType |
paulb@335 | 32 | |
paulb@335 | 33 | class MyResource: |
paulb@335 | 34 | |
paulb@335 | 35 | encoding = "utf-8" # We decide on "utf-8" as our chosen |
paulb@335 | 36 | # encoding. |
paulb@335 | 37 | def respond(self, trans): |
paulb@335 | 38 | [Do various things.] |
paulb@335 | 39 | |
paulb@335 | 40 | fields = trans.get_fields_from_body(encoding=self.encoding) # Explicitly use the encoding. |
paulb@335 | 41 | |
paulb@335 | 42 | [Do other things with the Unicode values from the fields.] |
paulb@335 | 43 | |
paulb@335 | 44 | trans.set_content_type(ContentType("text/html", self.encoding)) # The output Web page uses the encoding. |
paulb@335 | 45 | |
paulb@335 | 46 | [Produce the response, making sure that self.encoding is used to convert Unicode to raw strings.] </pre> |
paulb@335 | 47 | </body> |
paulb@335 | 48 | </html> |