<?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>Galen Grover - Connecticut Web Developer &#187; Python</title>
	<atom:link href="http://www.galengrover.com/cat/web-development/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.galengrover.com</link>
	<description>Sometimes I write things</description>
	<lastBuildDate>Fri, 03 Sep 2010 18:12:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Redirect non-www to www in django</title>
		<link>http://www.galengrover.com/web-development/python/redirect-non-www-to-www-in-django/</link>
		<comments>http://www.galengrover.com/web-development/python/redirect-non-www-to-www-in-django/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 16:57:31 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=661</guid>
		<description><![CDATA[In your settings file add


PREPEND_WWW = True




Then run syncdb


python manage.py syncdb




]]></description>
			<content:encoded><![CDATA[<p>In your settings file add</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">PREPEND_WWW = <span style="color: #008000;">True</span></pre></div></div>




<p>Then run syncdb</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">python manage.<span style="color: black;">py</span> syncdb</pre></div></div>




]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/python/redirect-non-www-to-www-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing dictionaries inside lists with Django templates</title>
		<link>http://www.galengrover.com/web-development/python/accessing-dictionaries-inside-lists-with-django-templates/</link>
		<comments>http://www.galengrover.com/web-development/python/accessing-dictionaries-inside-lists-with-django-templates/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 04:16:35 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=646</guid>
		<description><![CDATA[Click here to skip the story and straight to the code

Today while working on the new SVO i ran into an issue with the coding of breadcrumbs. I wanted to pull the breadcrumb code out of each page template and put it into the header template.

This is what i had originally in each template:


&#60;p id=&#34;breadcrumbs&#34;&#62;
 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="#dict_in_list">Click here to skip the story and straight to the code</a></p>

<p>Today while working on the new <a href="http://www.skatevideosonline.net">SVO</a> i ran into an issue with the coding of breadcrumbs. I wanted to pull the breadcrumb code out of each page template and put it into the header template.</p>

<p>This is what i had originally in each template:</p>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p id=&quot;breadcrumbs&quot;&gt;
    &lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;&lt;span class=&quot;bc_sep&quot;&gt;&gt;&lt;/span&gt;
    &lt;a href=&quot;{% url videos %}&quot;&gt;Videos&lt;/a&gt;&lt;span class=&quot;bc_sep&quot;&gt;&gt;&lt;/span&gt;
    &lt;a href=&quot;{% url company video.company.id video.company.name|slugify %}&quot;&gt;{{ video.company.name }}&lt;/a&gt;&lt;span class=&quot;bc_sep&quot;&gt;&gt;&lt;/span&gt;
    &lt;strong&gt;{{ video.name }}&lt;/strong&gt;
&lt;/p&gt;</pre></div></div>




<p>This quickly became annoying, repeating this code in every pag&#8217;es template. I decided to create a list of dictionaries inside each view to represent the breadcrumbs:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">breadcrumbs = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
breadcrumbs.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: black;">&#123;</span> <span style="color: #483d8b;">'home'</span>:reverse<span style="color: black;">&#40;</span> <span style="color: #483d8b;">'home'</span> <span style="color: black;">&#41;</span> <span style="color: black;">&#125;</span> <span style="color: black;">&#41;</span>
breadcrumbs.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: black;">&#123;</span> <span style="color: #483d8b;">'skaters'</span>:reverse<span style="color: black;">&#40;</span> <span style="color: #483d8b;">'skaters'</span> <span style="color: black;">&#41;</span> <span style="color: black;">&#125;</span> <span style="color: black;">&#41;</span>
breadcrumbs.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: black;">&#123;</span> index:<span style="color: #483d8b;">''</span> <span style="color: black;">&#125;</span> <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'index.html'</span>, <span style="color: black;">&#123;</span>...<span style="color: #483d8b;">'breadcrumbs'</span>:breadcrumbs<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>




<p>Then in my template i loop the list and extract the dictionary:</p>
<a name="dict_in_list"></a>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>p <span style="color: #008000;">id</span>=<span style="color: #483d8b;">&quot;breadcrumbs&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">for</span> breadcrumb <span style="color: #ff7700;font-weight:bold;">in</span> breadcrumbs <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span> <span style="color: #808080; font-style: italic;">#this loops the list</span>
	<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">for</span> text, link <span style="color: #ff7700;font-weight:bold;">in</span> breadcrumb.<span style="color: black;">items</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span> <span style="color: #808080; font-style: italic;">#this extracts the dictionary inside each list</span>
		<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">if</span> link <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
			<span style="color: #66cc66;">&lt;</span>a href=<span style="color: #483d8b;">&quot;{{ link }}&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> text|capfirst <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">&lt;</span>/a<span style="color: #66cc66;">&gt;&lt;</span>span <span style="color: #ff7700;font-weight:bold;">class</span>=<span style="color: #483d8b;">&quot;bc_sep&quot;</span><span style="color: #66cc66;">&gt;&gt;&lt;</span>/span<span style="color: #66cc66;">&gt;</span>
		<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
			<span style="color: #66cc66;">&lt;</span>strong<span style="color: #66cc66;">&gt;</span><span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> text|capfirst <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">&lt;</span>/strong<span style="color: #66cc66;">&gt;</span>
		<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endif <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
	<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endfor <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endfor <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
<span style="color: #66cc66;">&lt;</span>/p<span style="color: #66cc66;">&gt;</span></pre></div></div>




<p>The for loop seems like an &#8220;ugly&#8221; way to access the inner dictionary but i was unable to find a better method.</p>]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/python/accessing-dictionaries-inside-lists-with-django-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Python &#8211; Day 2</title>
		<link>http://www.galengrover.com/web-development/learning-python-day-2/</link>
		<comments>http://www.galengrover.com/web-development/learning-python-day-2/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 01:59:39 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[Learning Python]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=354</guid>
		<description><![CDATA[I&#8217;m excited about my new admin interface. Page 2 on the tutorial goes into how to customize the admin. I don&#8217;t care about that at the moment so I go to page 3.

Page 3 is about URLs. Django uses a sequence of tuples populated with regular expression -> url maps.  Great, this is how [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m excited about my new admin interface. Page 2 on the tutorial goes into how to customize the admin. I don&#8217;t care about that at the moment so I go to page 3.</p>

<p>Page 3 is about URLs. Django uses a sequence of <a href="http://diveintopython.org/getting_to_know_python/tuples.html">tuples</a> populated with regular expression -> url maps.  Great, this is how I did things in PHP. I add a couple of URLs and move on.</p>

<p>Next it&#8217;s time to create my views.  I write my first view which simple prints &#8220;Hi!&#8221;. It works, great. I see that the basics of the template language used in Django are very similar to how I do it in PHP.  I port my view from my PHP template to the template in Django.</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> foreach company <span style="color: #ff7700;font-weight:bold;">in</span> category.<span style="color: black;">company</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>




<p>Unfortunately this doesn&#8217;t work. It&#8217;s not looping over my categories/companies/videos.  I think it&#8217;s because it&#8217;s having trouble going backwards with my foreign key relationships.</p>

<p>I find that in order to go backwards you have to do this</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">for</span> company <span style="color: #ff7700;font-weight:bold;">in</span> category.<span style="color: black;">company_set</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>




<p>You have to use <em><strong>_set</strong></em> to access foreign key items. This doesn&#8217;t work either! I give up on this route and I try the method I use in PHP&#8230; creating a giant multi-dimensional array and then looping over that.</p>

<p>This is where I hit a wall.  I quickly find out that Python&#8217;s version of arrays, called dictionaries, can&#8217;t be created like they can in PHP. I don&#8217;t like that one bit.</p>

<p>Python doesn&#8217;t have a version of</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$video</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'company_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'category_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'video_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Video Name'</span><span style="color: #339933;">;</span></pre></div></div>




<p>I find some <a href="http://parand.com/say/index.php/2007/07/13/simple-multi-dimensional-dictionaries-in-python/">custom classes</a> via google, but Django won&#8217;t iterate over them.  A couple hours later I figure I need help.  I ask a couple of places and everyone suggests that it is possible with Django&#8217;s ORM, I just need to remove the ()</p>

<p>I find that you have to do this!</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">for</span> company <span style="color: #ff7700;font-weight:bold;">in</span> category.<span style="color: black;">company_set</span>.<span style="color: #008000;">all</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>




<p>You have to remove the () for functions that have no arguments. Works like a champ.  Two minutes later I have all categories/companies/videos displayed correctly.  I&#8217;m still using Django&#8217;s development server though so I have no styles/images, just black test on a white background.</p>

<p>I read somewhere that I should be using <a href="http://code.google.com/p/modwsgi/">mod_wsgi</a> instead of mod_python (I can&#8217;t find the source). Using <a href="http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango">this</a> I get it working with Django pretty quickly.</p>

<hr/>
<p><strong>Good:</strong>Django&#8217;s ORM</p> 
<p><strong>Bad:</strong> Python&#8217;s dictionary handling not my favorite. I enjoy the way PHP has implemented arrays and how they can be used as lists, dictionaries, stacks, etc.  Django&#8217;s documentation is thorough, but I get lost in the maze of versions it has.</p>
<p><strong>Notes:</strong> I don&#8217;t know why I didn&#8217;t think of this, but it&#8217;s been pointed out to me that you can do this:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a = <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
a<span style="color: black;">&#91;</span><span style="color: #483d8b;">'foo'</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
a<span style="color: black;">&#91;</span><span style="color: #483d8b;">'foo'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'bar'</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'this works'</span>: <span style="color: #483d8b;">'yes it does'</span><span style="color: black;">&#125;</span></pre></div></div>



<p>That probably would have solved my problem.</p>]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/learning-python-day-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Python &#8211; Day 1</title>
		<link>http://www.galengrover.com/web-development/learning-python-day-1/</link>
		<comments>http://www.galengrover.com/web-development/learning-python-day-1/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 09:26:01 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[Learning Python]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=333</guid>
		<description><![CDATA[Step one: Does my server have python? Yes.

Great, I have python. Now what? I don&#8217;t know. I google &#8220;python web development&#8221;. I click the first link.  Python webservers! I have to embed python into apache! Duh!


sudo apt-get install libapache2-mod-python




Now I have mod python running.  Two hours later I have a hello world on [...]]]></description>
			<content:encoded><![CDATA[<p>Step one: Does my server have python? <a href="http://www.linode.com">Yes</a>.</p>

<p>Great, I have python. Now what? I don&#8217;t know. I google &#8220;python web development&#8221;. I click the first link.  Python webservers! I have to embed python into apache! Duh!</p>


<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">sudo apt-get install libapache2-mod-python</pre></div></div>




<p>Now I have mod python running.  Two hours later I have a hello world on my development site.  That took way too long.  I&#8217;m annoyed.  I start asking people for recommendations. I just want to code! I&#8217;m sick of setting up my server.  With PHP it&#8217;s cake. One call to apt-get install and off you go.</p>

<p>98% of the people I ask recommend Django.  So I install django via apt-get install, head over to <a href="http://www.djangoproject.com/">djangoproject.com</a>, and start reading the <a href="http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01">tutorials</a>.</p>

<p>Page 1 is pretty straight forward.  I do some creation/configuring and create/activate my models.  Then I get to play with my models. Django&#8217;s ORM is pretty nice.  I usually prefer to write all my own SQL, but I could get used to this.</p>

<p>I get to page 2 of the tutorial where they start talking about the automatic admin. I&#8217;m shocked. This is amazing.  Are frameworks in PHP this awesome? I&#8217;ve always used my own PHP framework. I&#8217;ll have to check that out. Back to Django.</p>

<p>This admin app is amazing I have to try this with <a href="http://www.skatevideosonline.net">SVO</a>. I go back to page 1 on the tutorial and start following it but with SVO details.  I get to page 2 and then import all my data into my newly created tables.  I have a fully functioning admin in 10 minutes!</p>
<p>That&#8217;s all for day one.</p>
<hr/>
<p><strong>Good:</strong> Django, Django&#8217;s automatic admin</p>
<p><strong>Bad</strong>: Server setup not so easy</p>

<p><strong>Notes:</strong></p>
<p>1. To view the Django development server on a host other than localhost bind it to your server&#8217;s IP address</p>


<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">sudo python manage.py runserver 0.0.0.0:8000</pre></div></div>



]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/learning-python-day-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;ve decided to learn Python</title>
		<link>http://www.galengrover.com/web-development/ive-decided-to-learn-python/</link>
		<comments>http://www.galengrover.com/web-development/ive-decided-to-learn-python/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 08:47:16 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[Learning Python]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=323</guid>
		<description><![CDATA[I decided to learn Python by rewriting skate videos online in it.

I&#8217;m going to chronicle my experience right here&#8230; for noone to read =)
]]></description>
			<content:encoded><![CDATA[<p>I decided to learn Python by rewriting <a href="http://www.skatevideosonline.net">skate videos online</a> in it.</p>

<p>I&#8217;m going to chronicle my experience right here&#8230; for noone to read =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/ive-decided-to-learn-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
