1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/docs/encodings.html Sun Apr 10 21:11:25 2005 +0000
1.3 @@ -0,0 +1,48 @@
1.4 +<?xml version="1.0" encoding="iso-8859-1"?>
1.5 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1.6 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1.7 +<html xmlns="http://www.w3.org/1999/xhtml">
1.8 +<head>
1.9 + <title>Character Encodings</title>
1.10 + <meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" />
1.11 + <link href="styles.css" rel="stylesheet" type="text/css" />
1.12 +</head>
1.13 +
1.14 +<body>
1.15 +<h1>Character Encodings</h1>
1.16 +
1.17 +<p>WebStack tries to let applications work with Unicode as much as possible,
1.18 +but there are two places where plain Python strings can be involved:</p>
1.19 +<ul>
1.20 + <li>When <a href="responses.html">output is prepared</a> - for example, Web
1.21 + pages.</li>
1.22 + <li>When <a href="parameters.html">inspecting request parameters</a>.</li>
1.23 +</ul>
1.24 +
1.25 +<h2>Recommendations</h2>
1.26 +
1.27 +<p>Although WebStack has some support for detecting character encodings used
1.28 +in requests, it is often best for your application to exercise control over
1.29 +which encoding is used when <a href="parameters.html">inspecting request
1.30 +parameters</a> and when <a href="responses.html">producing responses</a>. The
1.31 +best way to do this is to decide which encoding is most suitable for the data
1.32 +presented and received in your application and then to use it throughout.
1.33 +Here is an outline of code which does this:</p>
1.34 +<pre>from WebStack.Generic import ContentType
1.35 +
1.36 +class MyResource:
1.37 +
1.38 + encoding = "utf-8" # We decide on "utf-8" as our chosen
1.39 + # encoding.
1.40 + def respond(self, trans):
1.41 + [Do various things.]
1.42 +
1.43 + fields = trans.get_fields_from_body(encoding=self.encoding) # Explicitly use the encoding.
1.44 +
1.45 + [Do other things with the Unicode values from the fields.]
1.46 +
1.47 + trans.set_content_type(ContentType("text/html", self.encoding)) # The output Web page uses the encoding.
1.48 +
1.49 + [Produce the response, making sure that self.encoding is used to convert Unicode to raw strings.] </pre>
1.50 +</body>
1.51 +</html>