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