<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Slacy's Blog &#187; django</title>
	<atom:link href="http://slacy.com/blog/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://slacy.com/blog</link>
	<description>This site is solar powered!</description>
	<lastBuildDate>Tue, 01 May 2012 05:50:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to set session variables in Django unit tests.</title>
		<link>http://slacy.com/blog/2012/01/how-to-set-session-variables-in-django-unit-tests/</link>
		<comments>http://slacy.com/blog/2012/01/how-to-set-session-variables-in-django-unit-tests/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 00:24:45 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1639</guid>
		<description><![CDATA[This was super non-obvious, and I lifted code from a couple of different places. If you&#8217;ve got Django view code that gets &#38; sets session values, you&#8217;ll want to test it properly, and the standard &#8220;self.client&#8221; from Django&#8217;s TestCase doesn&#8217;t really give you a usable session.  So, here&#8217;s the method I&#8217;m using: def stuff_session(client, dictionary): [...]]]></description>
			<content:encoded><![CDATA[<p>This was super non-obvious, and I lifted code from a couple of different places.</p>
<p>If you&#8217;ve got Django view code that gets &amp; sets session values, you&#8217;ll want to test it properly, and the standard &#8220;self.client&#8221; from Django&#8217;s TestCase doesn&#8217;t really give you a usable session.  So, here&#8217;s the method I&#8217;m using:</p>
<pre>def stuff_session(client, dictionary):
    """Given a client (self.client in a unit test TestCase) set the session to the contents of
    the dictionary given"""
    from django.conf import settings
    from django.utils.importlib import import_module
    engine = import_module(settings.SESSION_ENGINE)
    store = engine.SessionStore()
    store.save()  # we need to make load() work, or the cookie isworthless
    client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
    session = client.session
    session.update(dictionary)
    session.save()
    # and now remember to re-login!</pre>
<p>So, in my setUp() methods, I just call stuff_session(self.client, {&#8216;key&#8217;: &#8216;value&#8217;}) and it all works out great.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2012/01/how-to-set-session-variables-in-django-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>Django + PostgreSQL + virtualenv Development setup for Windows 7</title>
		<link>http://slacy.com/blog/2011/06/django-postgresql-virtualenv-development-setup-for-windows-7/</link>
		<comments>http://slacy.com/blog/2011/06/django-postgresql-virtualenv-development-setup-for-windows-7/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 19:09:51 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[installer]]></category>
		<category><![CDATA[pip]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[psycopg2]]></category>
		<category><![CDATA[virtualenv]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1562</guid>
		<description><![CDATA[Here&#8217;s what you need to do Django development on Windows 7. As I go through the install, I&#8217;m writing down all the steps to make sure that I don&#8217;t miss any. I&#8217;m going to focus on: Python 2.7.1 from python.org virtualenv (manages python packages and dependencies) Visual Studio 2008 (for compiling Python addons) PostgreSQL 8.4 [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s what you need to do Django development on Windows 7.  As I go through the install, I&#8217;m writing down all the steps to make sure that I don&#8217;t miss any.  I&#8217;m going to focus on:</p>
<ul>
<li><strong>Python 2.7.1</strong> from python.org</li>
<li><strong>virtualenv</strong> (manages python packages and dependencies)</li>
<li><strong>Visual Studio 2008</strong> (for compiling Python addons)</li>
<li><strong>PostgreSQL 8.4 </strong>(database engine)</li>
<li><strong>Django 1.3 </strong>(our web framework)</li>
<li>Windows <strong>psycopg2</strong> installer</li>
</ul>
<p>It will be possible to install any other requirements (PIL, etc.) using pip after the virtualenv is set up.</p>
<h2>Note on 32-bit versus 64-bit</h2>
<p>Most modern computers these days are 64-bit capable, and will (usually) be running a 64-bit operating system.  In addition, they can also run older 32-bit binaries.  When you install all the components below, you must choose either</p>
<h2>Install Visual C++ 2008 Express Edition with SP1</h2>
<p>This is the compiler tool needed to build other python extensions we&#8217;ll be adding later. Download it from ﻿<a href="http://microsoft.com/visualstudio/en-us/products/2008-editions/express">Microsoft</a>.   Select &#8220;Visual C++ 2008 Express Edition with SP1&#8243;, choose your Language and click the &#8220;Free Download&#8221; button.</p>
<p>You <strong>must</strong> use the 2008 edition, since that&#8217;s the compiler that was used to build Python 2.7.1, and the compiler versions <strong>must</strong> match for the additions to be compatible.  If you already have a newer version of Visual Studio installed, please also install the 2008 edition I&#8217;ve linked to.</p>
<p>You do not need to install the Silverlight runtime or Microsoft SQL Server, unless you think you want these for other purposes.</p>
<p>This compiler is only 32-bit capable, so we&#8217;ll be sticking to 32-bit Python below.  This shouldn&#8217;t cause any issues for most development installs.  If you have an official purchased version of Visual C++ 2008 that&#8217;s 64-bit, then you&#8217;re on your own.</p>
<h2>Install 32-bit Python 2.7.1 from python.org</h2>
<p>Start on <a href="http://python.org/getit">python.org</a> and select &#8220;Python 2.7.1 Windows Installer&#8221;.  Please <strong>do not</strong> choose the x86-64 Installer, as it isn&#8217;t compatible with the compiler from above.</p>
<p>Download and install python-2.7.1.msi</p>
<ul>
<li>Select &#8220;Install for all users&#8221;</li>
<li>Use the default location of &#8220;C:\Python27&#8243; (note: no period)</li>
<li>Use the default options on &#8220;Customize Python&#8221;.</li>
</ul>
<h2>Install setuptools from python.org</h2>
<p>setuptools is a Python package that facilitates installing other packages (and thus, bootstrapping your install system).  We&#8217;ll use setuptools to install other packages, but first we need to install it.  Get it from <a href="http://pypi.python.org/pypi/setuptools#downloads">python.org</a>.  I&#8217;m using &#8220;setuptools-0.6c11.win32-py2.7.exe&#8221;  Download and run that binary, and use the default options in the installer.</p>
<h2>Install PostgreSQL 8.4</h2>
<p>This will be your database server.  We use this version because it mirrors what we use in the production environment.  Select the <a href="http://enterprisedb.com/products-services-training/pgdownload#windows">8.4.8-1 installer for Windows</a>.</p>
<p>Run the installer and use the default install options. You&#8217;ll need to choose a password for the postgres user.  Choose something you won&#8217;t forget.</p>
<p>You do not need to run the &#8220;Stack Builder&#8221; tool.  Un-check that option and finish your install.</p>
<h2>Install psycopg2 for windows</h2>
<p><strong>psycopg2</strong> is the interface API from PostgreSQL to Python.  Unfortunately, it&#8217;s not packaged in a way that&#8217;s easy to install automatically, so you have to download and install it.  Choose the proper version for your Python (likely 2.7, 32-bit as we&#8217;ve discussed  before).  <a href="http://www.stickpeople.com/projects/python/win-psycopg/">Here&#8217;s a link to all the available packages.</a></p>
<h2>Install virtualenv</h2>
<p>virtualenv is a Python tools that helps us manage and install Python packages in a neat and clean way.  It also helps the installation of these packages to other systems (like to our deployment environment, which is likely Linux). Run:</p>
<pre>C:\&gt;cd \Python27\Scripts
C:\Python27\Scripts&gt;easy_install virtualenv</pre>
<p>From this point forward, we won&#8217;t use easy_install anymore.</p>
<h2>Create a development environment using virtualenv</h2>
<p>The virtualenv tool will create an &#8220;environment&#8221; where you can install any needed Python packages, like Django.  This environment contains specially modified versions of Python and other tools that make dependency management much, much easier.  Choose a directory for your environment.  I like to put things in /Home/&lt;username&gt;/Desktop/src/&lt;environment_name&gt;  for easy access.</p>
<pre>C:\Users\Steve Lacy\Desktop&gt;mkdir src
C:\Users\Steve Lacy\Desktop&gt;cd src
C:\Users\Steve Lacy\Desktop\src&gt;mkdir test
C:\Users\Steve Lacy\Desktop\src&gt;cd test
C:\Users\Steve Lacy\Desktop\src\test&gt;\Python27\Scripts\virtualenv --no-site-packages --distribute env</pre>
<p>This will create an environment in src\test\env and populate it with modified versions of Python, pip, etc.</p>
<p>You&#8217;ll need to &#8220;activate&#8221; this environment to start using it.  virtualenv puts in a simple activate script to help you do this, like this:</p>
<pre>C:\Users\Steve Lacy\Desktop\src\test&gt;env\Scripts\activate.bat
(env) C:\Users\Steve Lacy\Desktop\src\test&gt;</pre>
<p>Note how your prompt changed to say that you&#8217;re using development enviroment &#8220;env&#8221; which we named above.</p>
<p>Now that our environment is &#8220;active&#8221; we have easy access to the special versions of python and pip that have been placed in env/Scripts.  They&#8217;re now on your PATH, so you can use them directly.</p>
<h2>Install Django using pip</h2>
<p>This is fairly easy now that we&#8217;re in our environment and it&#8217;s active:</p>
<pre>(env) C:\Users\Steve Lacy\Desktop\src\test&gt;pip install django
<span style="font-size: 16px; color: #444444; font-family: Georgia, 'Bitstream Charter', serif; line-height: 24px; white-space: normal;">
</span></pre>
<p>Inspect the terminal output carefully to make sure that it successfully installs each of these.  Once that&#8217;s complete, you should be good to go.</p>
<p>You can create a new Django project by running:</p>
<pre>(env) C:\Users\Steve Lacy\Desktop\src\test&gt;python env\Scripts\django-admin.py</pre>
<p>From here, your best bet is to continue to the <a href="https://docs.djangoproject.com/en/1.3/intro/tutorial01/#creating-a-project">Django tutorial and introductions</a>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2011/06/django-postgresql-virtualenv-development-setup-for-windows-7/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
	</item>
		<item>
		<title>Cleaner pattern for  loops in Django templates?</title>
		<link>http://slacy.com/blog/2010/10/cleaner-pattern-for-loops-in-django-templates/</link>
		<comments>http://slacy.com/blog/2010/10/cleaner-pattern-for-loops-in-django-templates/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 22:26:15 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1371</guid>
		<description><![CDATA[I find myself typing stuff like this a lot: {% for item in list %} {% if forloop.first %}&#60;ul&#62;{% endif %} &#60;li&#62;{{ item.value }}&#60;/li&#62; {% if forloop.last %}&#60;/ul&#62;{% endif %} {% empty %} &#60;p&#62;No items in list.&#60;/p&#62; {% endif %} Is there a cleaner way to do this? It&#8217;s a lot of code for creating [...]]]></description>
			<content:encoded><![CDATA[<p>I find myself typing stuff like this a lot:</p>
<pre>{% for item in list %}
  {% if forloop.first %}&lt;ul&gt;{% endif %}
  &lt;li&gt;{{ item.value }}&lt;/li&gt;
  {% if forloop.last %}&lt;/ul&gt;{% endif %}
{% empty %}
  &lt;p&gt;No items in list.&lt;/p&gt;
{% endif %}</pre>
<p>Is there a cleaner way to do this?  It&#8217;s a lot of code for creating a simple loop.  I&#8217;m tempted to make a &#8220;listloop&#8221; class that wraps up all the ul and li logic right into the templatetag, but that seems like it hides a lot.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2010/10/cleaner-pattern-for-loops-in-django-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>Both production &amp; development settings using zc.buildout &amp; djangorecipe</title>
		<link>http://slacy.com/blog/2010/10/both-production-development-settings-using-zc-buildout-djangorecipe/</link>
		<comments>http://slacy.com/blog/2010/10/both-production-development-settings-using-zc-buildout-djangorecipe/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 22:56:30 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[production]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[recipe]]></category>
		<category><![CDATA[zc.buildout]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1352</guid>
		<description><![CDATA[It took me a while to figure this out, so I&#8217;m writing it down. What I wanted was to have my django.wsgi script use the production settings and to have bin/django use the development settings.  Sounds easy, right?  It&#8217;s not obvious what the right way to accomplish this is, but here&#8217;s what I&#8217;ve come up [...]]]></description>
			<content:encoded><![CDATA[<p>It took me a while to figure this out, so I&#8217;m writing it down.</p>
<p>What I wanted was to have my django.wsgi script use the production settings and to have bin/django use the development settings.  Sounds easy, right?  It&#8217;s not obvious what the right way to accomplish this is, but here&#8217;s what I&#8217;ve come up with for my zc.buildout script:</p>
<pre>[buildout]
parts = django djangoprod

[django]
recipe = djangorecipe
version = 1.2
settings = development
project = project 

[djangoprod]
recipe = djangorecipe
version = ${django:version}
settings = production
wsgi = true
project = ${django:project}</pre>
<p>By specifying 2 different djangorecipe based rules, we can create both bin/django and bin/djangoprod.wsgi.  djangoprod.wsgi will use production settings, and bin/django will use development settings, which is in general, what you want for a develop &amp; deploy buildout script.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2010/10/both-production-development-settings-using-zc-buildout-djangorecipe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
	</item>
		<item>
		<title>Django development setup on Windows</title>
		<link>http://slacy.com/blog/2010/09/django-development-setup-on-windows/</link>
		<comments>http://slacy.com/blog/2010/09/django-development-setup-on-windows/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 00:19:18 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[setuptools]]></category>
		<category><![CDATA[visual studio express]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[zc.buildout]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1341</guid>
		<description><![CDATA[Django development on Windows sounds easy, but there are a bunch of steps to get it all working right. Python.  Use Python 2.7 from python.org ﻿http://python.org/download/.  I recommend avoiding the 3rd party Python distributions (ActiveState, Enthought, etc.) and sticking with the official build.  As a note, this python is (currently) built with Video Studio 2008. [...]]]></description>
			<content:encoded><![CDATA[<p>Django development on Windows sounds easy, but there are a bunch of steps to get it all working right.</p>
<ul>
<li>Python.  Use <strong>Python 2.7</strong> from python.org ﻿<a href="http://python.org/download/">http://python.org/download/</a>.  I recommend avoiding the 3rd party Python distributions (ActiveState, Enthought, etc.) and sticking with the official build.  As a note, this python is (currently) built with Video Studio 2008.</li>
<li><strong>Visual Studio Express 2008</strong>.  Note the 2008 part.  The version Python is built with <strong>much</strong> match the version used to build extensions.  This is currently still available from the Microsoft download site, but is a bit buried under a couple links.  When installing, don&#8217;t bother installing SQL Server 2008.  You won&#8217;t need it.</li>
<li><strong>PostgreSQL 8.4</strong>.  I chose this version because it most closely matched my Linux deployment.  Download and run the installer from <a href="http://www.enterprisedb.com/products/pgdownload.do#windows">http://www.enterprisedb.com/products/pgdownload.do#windows</a> and make sure to choose version 8.4.</li>
<li><strong>setuptools</strong> from <a href="http://pypi.python.org/pypi/setuptools#windows">http://pypi.python.org/pypi/setuptools#windows</a>.  This will allow you to easily download and install other package add-ons.</li>
</ul>
<p>Next, you&#8217;ll need to manually modify your PATH to include Python &amp; PostgreSQL.    Go to Control Panel-&gt;System-&gt;Advanced system settings-&gt;Environment Variables, and make sure that your Path contains:</p>
<p>﻿C:\Python27\;C:\Python27\Scripts;C:\Program Files\PostgreSQL\8.4\bin</p>
<p><span style="font-size: 15.6px;">Then, you&#8217;ll need to install some extra Python libraries:</span></p>
<p>C:\&gt; easy_install pil<br />
C:\&gt; easy_install psycopg2</p>
<p>Then, you can run your zc.buildout script to bring in everything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2010/09/django-development-setup-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>Multi-dimensional dicts in Python &amp; Django?</title>
		<link>http://slacy.com/blog/2010/04/multi-dimensional-dicts-in-python-django/</link>
		<comments>http://slacy.com/blog/2010/04/multi-dimensional-dicts-in-python-django/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 18:24:02 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[default]]></category>
		<category><![CDATA[dict]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1144</guid>
		<description><![CDATA[The code I&#8217;m working on frequently uses multi-dimensional dicts in Python.  There is a bunch of code that seems to be always doing this: # a,b, and c have come from some external data source, QuerySet, etc. a = 'axis_1' b = 'axis_2' c = 'axis_3' d = 'value' if a not in context: context[a] [...]]]></description>
			<content:encoded><![CDATA[<p>The code I&#8217;m working on frequently uses multi-dimensional dicts in Python.  There is a bunch of code that seems to be always doing this:</p>
<pre># a,b, and c have come from some external data source, QuerySet, etc.
a = 'axis_1'
b = 'axis_2'
c = 'axis_3'
d = 'value'
if a not in context:
  context[a] = {}
if b not in context:
  context[a][b] = {}
if c not in context:
  context[a][b][c] = value
</pre>
<p>Or some variant thereof.  Is there a better pattern for multi-dimensional (arbitrary) dicts in Python/Django?  The setdefault() method doesn&#8217;t have the right semantics for this operation, and I&#8217;m not sure that it would actually work right for multi-dimensional arrays anyway. </p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2010/04/multi-dimensional-dicts-in-python-django/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
	</item>
		<item>
		<title>Announcing Parents Guild</title>
		<link>http://slacy.com/blog/2009/12/announcing-parents-guild/</link>
		<comments>http://slacy.com/blog/2009/12/announcing-parents-guild/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 00:41:06 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[parenting]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[site]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1029</guid>
		<description><![CDATA[Just a quick note to my readers and searchers that we&#8217;ve launched our new parenting website, Parents Guild.  We&#8217;ve got question &#38; answering, e-mail notifications, tagging, voting, and a bunch of other parenting-specific features in the works.  We&#8217;ve got about 50 users so far, which is pretty great considering we haven&#8217;t done much marketing.   [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick note to my readers and searchers that we&#8217;ve launched our new parenting website, <a href="http://parentsguild.com">Parents Guild</a>.  We&#8217;ve got question &amp; answering, e-mail notifications, tagging, voting, and a bunch of other parenting-specific features in the works.  We&#8217;ve got about 50 users so far, which is pretty great considering we haven&#8217;t done much marketing. <img src='http://slacy.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />    We&#8217;re welcoming feedback (use the link on the left hand side of the site, or comment here, or comment on the blog or facebook).</p>
<p>Our goal is to make the site as welcoming and informative to as many people as possible.  That includes Dads, Grandparents, parents of older children, people without children, as well as expecting couples.  Come on by and share ideas &amp; advice!</p>
<p><a href="http://parentsguild.com/account/register/">It&#8217;s easy to sign up</a> and ask or answer a question!</p>
<p>We also have a <a href="http://blog.parentsguild.com">blog</a> and a <a href="http://www.facebook.com/pages/Parents-Guild/189697752023?ref=nf">facebook page</a>, so you can follow what we&#8217;re doing on the site.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2009/12/announcing-parents-guild/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>Resolving circular model imports caused by Haystack &amp; Django</title>
		<link>http://slacy.com/blog/2009/11/resolving-circular-model-imports-caused-by-haystack-django/</link>
		<comments>http://slacy.com/blog/2009/11/resolving-circular-model-imports-caused-by-haystack-django/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 19:22:05 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[circular import]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[haystack]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1013</guid>
		<description><![CDATA[So, in converting our search to Haystack, I&#8217;ve found that some of my standalone maintainence scripts no longer work.  They had an import error trying to import some of the models. The issue is that I&#8217;ve updated my models.py with some Haystack code.  The code at hand is in the save() method of the model [...]]]></description>
			<content:encoded><![CDATA[<p>So, in converting our search to Haystack, I&#8217;ve found that some of my standalone maintainence scripts no longer work.  They had an import error trying to import some of the models.</p>
<p>The issue is that I&#8217;ve updated my models.py with some Haystack code.  The code at hand is in the save() method of the model object, and basically looks like this:</p>
<pre name="code" class="python">[... other misc django imports above ... ]
from haystack import site

class MyModel(models.Model):
    [ ... fields of model, etc. ... ]
    def save(self, force_insert=False, force_update=False):
        super(MyModel, self).save(force_insert, force_update)
        if self.is_child_node():
            site.get_index(MyModel).update_object(self.parent)</pre>
<p>The problem was that if I had a standalone script that said:</p>
<pre name="code" class="python">from mysite.models import MyModel</pre>
<p>Then I would get a circular import, because the &#8220;from haystack import site&#8221; line ends up working it&#8217;s way over to search_sites.py that says &#8220;haystack.autodiscover()&#8221; which ends up looking for all search_indexes.py, and one of these says &#8220;from myapp.models import MyModel&#8221;.  So, there&#8217;s your circular reference.</p>
<p>The solution to this problem is to put a haystack import at the top of my standalone script, before I import my model.  Adding &#8220;from haystack import site&#8221; works, even though the code therein doesn&#8217;t actually use haystack.</p>
<p>I think the haystack code here:</p>
<pre>/usr/lib/python2.5/site-packages/haystack/__init__.py in autodiscover()
 88         # Step 3: import the app's search_index file. If this has errors we want them
 89         # to bubble up.
---&gt; 90         __import__("%s.search_indexes" % app)
 91
 92 # Make sure the site gets loaded.</pre>
<p>Should have a try/except block around it, and then this extra import wouldn&#8217;t be necessary.  The funny thing is that looking at the source, all the other imports there catch ImportError and don&#8217;t pass it back up, but this case does.</p>
<p>I was able to simply debug this in ipython my running &#8220;from myapp.models import MyModel&#8221; and debugging, per above.  It was fairly clear that it&#8217;s a circular import.  I&#8217;m surprised (in general) that Python doesn&#8217;t handle circular imports any more gracefully than saying &#8220;&lt;type &#8216;exceptions.ImportError&#8217;&gt;: cannot import name MyModel&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2009/11/resolving-circular-model-imports-caused-by-haystack-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>pprinting the Django context object</title>
		<link>http://slacy.com/blog/2009/10/pprinting-the-django-context-object/</link>
		<comments>http://slacy.com/blog/2009/10/pprinting-the-django-context-object/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 22:45:15 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[pprint]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=988</guid>
		<description><![CDATA[This was non-obvious to me for quite some time, and I tried several different approaches before settling on this one: If you would like to pprint your Django Context (or RequestContext) object from within a template itself, there are several approaches you can take.  The easiest way I found is to say: context['context'] = context [...]]]></description>
			<content:encoded><![CDATA[<p>This was non-obvious to me for quite some time, and I tried several different approaches before settling on this one:</p>
<p>If you would like to pprint your Django Context (or RequestContext) object from within a template itself, there are several approaches you can take.  The easiest way I found is to say:</p>
<pre name="code" class="python">context['context'] = context</pre>
<p>in your view(s) that you want to display, and then in the template, you can say:</p>
<pre name="code" class="html">{% for c in context %}
&lt;p&gt;{{ c|pprint }}&lt;/p&gt;
{% endfor %}</pre>
<p>Or something along those lines.</p>
<p>The reason that the simple {{ context|pprint }} doesn&#8217;t output what you expect is because of the __repr__() method on the Context() object in Django.  It constructs a big huge string for you, when what you want is to let pprint do all the heavy lifting and indentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2009/10/pprinting-the-django-context-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>How to split views.py into a directory (Django, Python)</title>
		<link>http://slacy.com/blog/2009/07/how-to-split-views-py-into-a-directory-django-python/</link>
		<comments>http://slacy.com/blog/2009/07/how-to-split-views-py-into-a-directory-django-python/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 23:35:04 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=864</guid>
		<description><![CDATA[So, I&#8217;ve been doing some Django programming on the side, and the thing that annoys me is that Django forces you to put nearly all your source code into 2 files:  models.py and views.py Being a reasonable person, I wanted to have a separate file for each view in my system.  In other words, I [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ve been doing some Django programming on the side, and the thing that annoys me is that Django forces you to put nearly all your source code into 2 files:  models.py and views.py</p>
<p>Being a reasonable person, I wanted to have a separate file for each view in my system.  In other words, I want to create a views directory, with __init__.py inside, and my view files in there.  Not being a Python expert, I found this harder than expected.  Here&#8217;s what you need to do:</p>
<ol>
<li>Create a views directory.</li>
<li>Split views.py into one file per method in the new views directory.</li>
<li>Edit views/__init__.py and for each view, say &#8220;from myview import *&#8221;</li>
<li>Use your views as you previously did in urls.py</li>
</ol>
<p>I&#8217;ve heard some rumblings that you could create an __init__.py that did something like go through every file in the current directory and import everything there.  That seems a bit over the top, and I&#8217;m happy to manage the imports in __init__.py for now.</p>
<p>I believe a very similar technique should work for Models, but I haven&#8217;t tested it.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2009/07/how-to-split-views-py-into-a-directory-django-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
	</item>
	</channel>
</rss>

