<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog from the Loft &#124; PK Shiu Personal Blog &#187; django python</title>
	<atom:link href="http://www.pkshiu.com/loft/archive/tag/django-python/feed" rel="self" type="application/rss+xml" />
	<link>http://www.pkshiu.com/loft</link>
	<description>Thoughts without walls</description>
	<lastBuildDate>Sun, 05 Feb 2012 01:50:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Django and PyTextile Revisited</title>
		<link>http://www.pkshiu.com/loft/archive/2008/07/django-and-pytextile-revisited</link>
		<comments>http://www.pkshiu.com/loft/archive/2008/07/django-and-pytextile-revisited#comments</comments>
		<pubDate>Tue, 01 Jul 2008 22:02:09 +0000</pubDate>
		<dc:creator>pk</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[django python]]></category>

		<guid isPermaLink="false">http://www.pkshiu.com/loft/archive/2008/07/django-and-pytextile-revisited</guid>
		<description><![CDATA[I wrote a post earlier about PyTextile not working well in Django. James Bennet was nice enough to add some pointers to the issue. (Comments are working now on my Blog). Now that I am using textile more, I want to investigate and document the issue better. Most importantly, the problem exists only with the0.96x [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a <a href="http://www.pkshiu.com/loft/archive/2007/10/using-pytextile-in-django-problem-with-unicode">post earlier </a>about PyTextile not working well in Django. <a href="http://www.b-list.org/links/2007/oct/07/pkshiu-pytextile/">James Bennet</a> was nice enough to add some pointers to the issue. (Comments are working now on my Blog). Now that I am using textile more, I want to investigate and document the issue better.</p>
<p>Most importantly, the problem exists only with the<strong>0.96x version</strong> of Django. If you are up to date with trunk, you can skip this article.</p>
<p><strong>The issues are as follows:</strong><br />
- the textile markup.py code in Django 0.96x assumes that input to textile is in the Django default encoding, which usually is utf-8<br />
- Django database typically uses utf-8 encoding for storage, but<br />
- Python2.5 + Django 0.96x is mostly unicode</p>
<p>Considering the following sources of data for used by the &#8220;textile&#8221; filter in a template, only the last case, using clean_data, will break:</p>
<p>1. Direct objects retrieved from database, works fine.<br />
2. Browser input via GET, works fine<br />
3. Browser input via form POST, accessed <strong>not</strong> using clean_data, works fine<br />
4. The clean_data pseudo dictionary returns data in unicode, and will break the textile filter.</p>
<p><strong>Code Example:</strong><br />
# view:<br />
obj = ModelClass.objects.get(pk=id)<br />
# template:<br />
{{ obj.textile }} works</p>
<p># view:<br />
field = request.GET.get(&#8216;some_field&#8217;)<br />
# template:<br />
{{ field | textile }} works</p>
<p># view:<br />
field = request.POST.get(&#8216;some_field&#8217;)<br />
# template:<br />
{{ field | textile }} works</p>
<p># view:<br />
form = TestForm(request.POST)<br />
field = form.clean_data['some_field']<br />
# template:<br />
{{ field | textile }} gives:</p>
<p>Exception Type:  	UnicodeDecodeError<br />
Exception Value: 	&#8216;ascii&#8217; codec can&#8217;t decode byte 0xb4 in position 0: ordinal not in range(128)</p>
<p><strong>Implication</strong><br />
A very common pattern for object editing is to let the user input data for a field, saves it to the database, and finish with a template displaying the updated data. Unfortunately, since the database object is updated with the form.clean_data input, it will break on the display template after saving to the database.</p>
<p><strong>Solution</strong><br />
There are two possible solutions. The first one that I proposed earlier is for force the data encoding to utf-8 when assigning a model field from clean_data. e.g.:</p>
<p>my_object.field = clean_data['field_name'].encode(&#8216;utf-8&#8242;)</p>
<p>A better solution, is to encapsulate the textile processing in a function. Do not force the template author even know that we are using textile. Before Django 1.0, this function will do the magic utf-8 conversion. At Django 1.0, the function can simply call pytextile.</p>
<pre class="brush: python; title: ; notranslate">
class MyClass(models.Model):
    body = models.CharField()

    def body_formatted(self):
        import textile
        return textile.textile( self.body.encode('utf-8'),
                encoding='utf-8', output='utf-8')
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pkshiu.com/loft/archive/2008/07/django-and-pytextile-revisited/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

