<?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>Perplexed Labs &#187; ternary</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/ternary/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Mon, 16 May 2011 14:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Adventures in Django and Python &#8211; Part III</title>
		<link>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/</link>
		<comments>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 14:00:38 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[ternary]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=307</guid>
		<description><![CDATA[Read my previous two posts on Django and Python - Part I and Part II I've been working on a project management tool suite in Django. It's been a great side project to really experiment with Django in real-world scenarios. Forms At times I feel like I fight with newforms. In particular, it lacks the [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/04/22/django-url-parameter-passing-and-python-strings/' rel='bookmark' title='Permanent Link: Django URL Parameter Passing and Python Strings'>Django URL Parameter Passing and Python Strings</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/03/20/django-and-python-first-impressions-part-ii/' rel='bookmark' title='Permanent Link: Django and Python First Impressions &#8211; Part II'>Django and Python First Impressions &#8211; Part II</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/08/getting-started-with-django-and-python-first-impressions/' rel='bookmark' title='Permanent Link: Getting Started with Django and Python &#8211; First Impressions'>Getting Started with Django and Python &#8211; First Impressions</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><em>Read my previous two posts on Django and Python - <a href="http://blog.perplexedlabs.com/2009/02/08/getting-started-with-django-and-python-first-impressions/">Part I</a> and <a href="http://blog.perplexedlabs.com/2009/03/20/django-and-python-first-impressions-part-ii/">Part II</a></em></p>
<p>I've been working on a project management tool suite in Django.  It's been a great side project to really experiment with Django in real-world scenarios.</p>
<h3>Forms</h3>
<p>At times I feel like I fight with newforms.  In particular, it lacks the ability to specify basic class or style attributes for a given form field from within the template.  I'd like to be able to more finely tune the display of the field, directly within the template, with a style attribute or a class.  Is it suggested you write your own custom form field widget for a single element?  I've been getting around this by doing the following:</p>
<pre class="brush: python; title: ;">
&lt;input type=&quot;text&quot; name=&quot;{{ todo_form.item.name }}&quot; style=&quot;width: 720px;&quot;/&gt;
</pre>
<p>This gets more complicated if you want to set a style attribute for a form field that's a select box (for a ForeignKey model field, for example).  </p>
<pre class="brush: python; title: ;">
&lt;label for=&quot;category&quot;&gt;Category:&lt;/label&gt; &lt;select name=&quot;{{ category_form.project.name }}&quot; style=&quot;width: 221px;&quot;&gt;
{% for choice_val, choice_label in category_form.project.field.choices %}
	&lt;option value=&quot;{{ choice_val }}&quot;&gt;{{ choice_label }}&lt;/option&gt;
{% endfor %}
</pre>
<p>Is this a good use case for template tags?  I feel like I'm missing something here.</p>
<p>On the positive side, it was an absolute pleasure to work with multiple forms on a single page submitted to and processed by a single view.  This is primarily thanks to prefixes.  Excellent, that's how easy it should be.</p>
<h3>Ternary Operator</h3>
<p><strong>Update:</strong> <em>It's been pointed out in comments (thanks!) that Python 2.5 introduced a ternary operator.  It's syntax is as follows:</em></p>
<pre class="brush: python; title: ;">
label = &quot;true&quot; if booleanVariable else &quot;false&quot;
</pre>
<p>I also ran into a minor Python syntax issue.  I love the ternary operator in languages that offer it.  It's a concise, one-line, syntax for an if-else clause.  Consider the following PHP code:</p>
<pre class="brush: php; title: ;">
$label = $booleanVariable ? 'true' : 'false';

// the above is identical to the following block:
if($booleanVariable) {
   $label = 'true';
} else {
   $label = 'false';
}
</pre>
<p>Python unfortunately lacks this syntactic sugar.  Fortunately, however, you can effectively accomplish the same thing by doing this:</p>
<pre class="brush: python; title: ;">
label = (booleanVariable and 'true' or 'false')

# the above is equivalent to the following block:
if booleanVariable:
   label = 'true'
else:
   label = 'false'
</pre>
<h3>Sessions</h3>
<p>Django has built in support for sessions.  By default, sessions last longer than the lifecycle of the user's browser.  I personally think it should be the other way around.  It's easily changed though (in your settings.py):</p>
<pre class="brush: python; title: ;">
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
</pre>
<h3>Views</h3>
<p>In one of my views I wanted to test whether a filtered result set was empty or not.  I was curious whether this was the "pythonic" way to accomplish this:</p>
<pre class="brush: python; title: ;">
account = get_object_or_404(Account, pk=account_id)
if account.useraccount_set.filter(user__exact=request.user) != []:
</pre>
<p>Also, with respect to views and passing context to the response, sometimes it's an efficient shortcut to use <strong>locals()</strong> instead of explicitly typing out all the variables you'd like to expose.  locals() returns a dictionary of all variables defined within the local scope.</p>
<pre class="brush: python; title: ;">
def myview(request, id):
    account = get_object_or_404(Account, pk=id)
    new_account_form = NewAccountForm()

    return render_to_response('myview.html', locals())
</pre>
<p>More soon!</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/04/22/django-url-parameter-passing-and-python-strings/' rel='bookmark' title='Permanent Link: Django URL Parameter Passing and Python Strings'>Django URL Parameter Passing and Python Strings</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/03/20/django-and-python-first-impressions-part-ii/' rel='bookmark' title='Permanent Link: Django and Python First Impressions &#8211; Part II'>Django and Python First Impressions &#8211; Part II</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/08/getting-started-with-django-and-python-first-impressions/' rel='bookmark' title='Permanent Link: Getting Started with Django and Python &#8211; First Impressions'>Getting Started with Django and Python &#8211; First Impressions</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/08/13/adventures-in-django-and-python-part-iii/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

