<?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>All the Good Titles Were Taken - Chris Scott &#187; Django</title>
	<atom:link href="http://iamzed.com/category/tech/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamzed.com</link>
	<description>Chris Scott's Blog</description>
	<lastBuildDate>Thu, 07 Jan 2010 20:51:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>A Primer on virtualenv</title>
		<link>http://iamzed.com/2009/05/07/a-primer-on-virtualenv/</link>
		<comments>http://iamzed.com/2009/05/07/a-primer-on-virtualenv/#comments</comments>
		<pubDate>Thu, 07 May 2009 20:11:37 +0000</pubDate>
		<dc:creator>Chris Scott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[virtualenv]]></category>

		<guid isPermaLink="false">http://iamzed.com/?p=1293</guid>
		<description><![CDATA[<h2>What is virtualenv and Why Should You Use It?</h2>
<p>Put simply (and copied from the project page), <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a> is a tool to create isolated Python environments. Why is this good? You can create a new Python environment to run a Python/Django/whatever app&#8230;</p>]]></description>
			<content:encoded><![CDATA[<h2>What is virtualenv and Why Should You Use It?</h2>
<p>Put simply (and copied from the project page), <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a> is a tool to create isolated Python environments. Why is this good? You can create a new Python environment to run a Python/Django/whatever app and install all package dependencies into the virtualenv without affecting your system's site-packages. Need to upgrade a package to see how it affects your app? Create a new virtualenv, install/copy your app into it, run your tests, and delete it when you are done. Just like git makes branching inexpensive and easy, virtualenv makes creating and managing new Python environments inexpensive and easy.</p>
<h2>Show Me Some Code!</h2>
<p>First, you need to install virtualenv. The typical way to install is from PyPi using <tt>easy_install</tt>. You can also use <tt>pip</tt> or install from source. We'll use <tt>easy_install</tt> since it is&#8230;easy:</p>
<div class="dean_ch" style="white-space: wrap;">$ <span class="kw2">sudo</span> easy_install virtualenv<br />
Password:</p>
<p>Searching <span class="kw1">for</span> virtualenv<br />
Best match: virtualenv <span class="nu0">1.3</span><span class="nu0">.3</span><br />
Downloading http://pypi.python.org/packages/<span class="nu0">2.5</span>/v/virtualenv/virtualenv<span class="nu0">-1.3</span><span class="nu0">.3</span>-py2<span class="nu0">.5</span>.egg<span class="re3">#<span class="re2">md5=</span>fae350c941cd9eadf5e9a407c37a2e03Processing virtualenv<span class="nu0">-1.3</span><span class="nu0">.3</span>-py2<span class="nu0">.5</span>.egg</span><br />
Adding virtualenv <span class="nu0">1.3</span><span class="nu0">.3</span> to easy-<span class="kw2">install</span>.pth <span class="kw2">file</span><br />
Installing virtualenv script to /usr/<span class="kw3">local</span>/bin</p>
<p>Using /Library/Python/<span class="nu0">2.5</span>/site-packages/virtualenv<span class="nu0">-1.3</span><span class="nu0">.3</span>-py2<span class="nu0">.5</span>.egg<br />
Processing dependencies <span class="kw1">for</span> virtualenv<br />
Finished processing dependencies <span class="kw1">for</span> virtualenv</div>
<p>Now, you're ready to create your virtualenv. BUT FIRST&#8230;you have a decision to make. Do you want this virtualenv to use packages from your system site-packages on install them in the virtualenv's site-packages? By default, virtualenv will symlink to your system's site-packages if you install a package in the virtualenv that is already installed on your system. If you want a totally (well, as much as is possible) isolated virtualenv, you'll want to do the latter. To do this, you pass in the --no-site-packages switch when creating your virtualenv.</p>
<p>So, let's create a virtualenv named for the project we are working on (mycoolproject) and tell it not to use the system site-packages but keep it's own copy:</p>
<div class="dean_ch" style="white-space: wrap;">$ <span class="kw3">pwd</span><br />
/Users/chris/Documents/clients</p>
<p>$ virtualenv --no-site-packages mycoolproject<br />
New python executable <span class="kw1">in</span> mycoolproject/bin/python<br />
Installing setuptools&#8230;&#8230;&#8230;&#8230;<span class="kw1">done</span>.</p>
<p>$ <span class="kw2">ls</span> mycoolproject<br />
bin &nbsp; &nbsp; include lib</div>
<p>I told virtualenv to not use the system <tt>site-packages</tt> and to create the virtualenv in a directory named <tt>mycoolproject</tt>. This directory didn't exist so it created it, however, if it did exist virtualenv would install it's files within it. virtualenv created three directories to use for running Python and installing packages into the virtualenv.</p>
<p>So, we have a new virtualenv, now what? Activate your virtualenv. To do this, change to the virtualenv directory you just created and source the activation script from the <tt>bin</tt> directory in your virtualenv:</p>
<div class="dean_ch" style="white-space: wrap;">$ <span class="kw3">cd</span> mycoolproject<br />
chris@chris.<span class="kw3">local</span> <span class="br0">&#91;</span>~/Documents/clients/mycoolproject<span class="br0">&#93;</span><br />
$ <span class="kw3">source</span> bin/activate<br />
<span class="br0">&#40;</span>mycoolproject<span class="br0">&#41;</span>chris@chris.<span class="kw3">local</span> <span class="br0">&#91;</span>~/Documents/clients/mycoolproject<span class="br0">&#93;</span><br />
$</div>
<p>I've included my full command prompt here so you can see that when you activate a virtualenv, it modifies your current prompt to prepend the name of the virtualenv in parentheses so that you know at a glance you have it active.</p>
<p>So, what happened here? Other than the prompt change, the main changes the activate script makes are:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re2">VIRTUAL_ENV=</span><span class="st0">&quot;/Users/chris/Documents/clients/mycoolproject&quot;</span><br />
<span class="kw3">export</span> VIRTUAL_ENV</p>
<p><span class="re2">_OLD_VIRTUAL_PATH=</span><span class="st0">&quot;$PATH&quot;</span><br />
<span class="re2">PATH=</span><span class="st0">&quot;$VIRTUAL_ENV/bin:$PATH&quot;</span><br />
<span class="kw3">export</span> PATH</div>
<p>This sets up an environment variable with the virtualenv path and also modifies your path so the <tt>bin</tt> directory in the virtualenv is first. This is important since running python from the command line when your virtualenv is active runs it from there instead of your system path. This means through the magic of the Python <a href="http://docs.python.org/library/site.html">site module</a> the <tt>site-packages</tt> in your virtualenv will be in <tt>sys.path</tt> and not your system's <tt>site-packages</tt>.</p>
<h2>Installing Packages in a virtualenv</h2>
<p>So, this is where the fun part starts. If you look at the bin directory in your virtualenv, you'll see easy_install which has been modified to put eggs and packages in the virtualenv's site-packages directory. So, once you activate your virtualenv installing a package in it is as easy as always:</p>
<div class="dean_ch" style="white-space: wrap;">$ easy_install ipython</div>
<p>Notice that I didn't have to <tt>sudo</tt> this time since the files will all be installed in the virtualenv's  <tt>lib/python2.5/site-packages</tt> directory which was created as my user account so I have the appropriate permissions.</p>
<p>You can also use <a href="http://pypi.python.org/pypi/pip">pip</a> to install packages in a virtualenv since it is virtualenv-aware. This is very handy since you can easily create a pip requirements file and use that to easily install all the various packages the software you are running in your virtualenv will need. Check out the <a href="http://pinaxproject.com/docs/dev/install.html#development-instructions">Pinax project's installation docs</a> for an example of this.</p>
<h2>Tips for Working With virtualenv</h2>
<ul>
<li>Use the <a href="http://www.doughellmann.com/projects/virtualenvwrapper/">virtualenvwrapper</a> script to make working with and changing to/from multiple virtualenvs easy.</li>
<li>For Django, after you install Django in your virtualenv, copy the <tt>django-admin.py</tt> from the Django package to the <tt>bin</tt> directory of your virtualenv so that you can still type <tt>django-admin.py startproject</tt> without specifying the full path to <tt>django-admin.py</tt>.</li>
<li>If you use the <tt>--no-site-packages</tt> option you are starting with a bare Python install and will need to install all the things you have forgotten about since the first time you set your system up like PIL, ipython, ipdb, readline, etc. This is where a <tt>pip</tt> requirements file is a very good thing.</li>
<li>If you are on OS X installing PIL from <tt>pip</tt> or <tt>easy_install</tt> may not work. To get around this just symlink the <tt>PIL</tt> directory from <tt>/Library/Python/2.5/site-packages</tt> to <tt>lib/python2.5/site-packages</tt> in your virtualenv.</li>
</ul>
<h2>Questions, Corrections, Tips?</h2>
<p>I'm not a virtualenv expert, I just play one on the internet. If you see anything wrong with what I've said or have any suggestions or tips, please let me know in the comments or email chris@ this domain.</p>
]]></content:encoded>
			<wfw:commentRss>http://iamzed.com/2009/05/07/a-primer-on-virtualenv/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Local to External Django Docs Bookmarklet</title>
		<link>http://iamzed.com/2009/04/07/local-to-external-django-docs-bookmarklet/</link>
		<comments>http://iamzed.com/2009/04/07/local-to-external-django-docs-bookmarklet/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 12:53:11 +0000</pubDate>
		<dc:creator>Chris Scott</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://iamzed.com/?p=1283</guid>
		<description><![CDATA[<p>As I'm learning Django, I've found it helpful to have a local copy of the Django docs to refer to. This is quicker than going to the Django docs site and also helps when I don't have a connection. The&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>As I'm learning Django, I've found it helpful to have a local copy of the Django docs to refer to. This is quicker than going to the Django docs site and also helps when I don't have a connection. The only drawback to this is that when I find something I want to share with someone I have to go find it again in the online docs and send them a link.</p>
<p>I finally got tired of this and created a simple bookmarklet that will take me from my local copy to the same place in the online docs. Here's it is: <a title="Django External Docs" href="javascript:var%20orig=location.href;location.replace(orig.replace(/.*_build\/html\/(.*)(\.html)(.*)/, 'http://docs.djangoproject.com/en/dev/$1/$3'));">Django Docs Link</a>. Just drag that to your browser's toolbar. When you are browsing your local Django docs, click on it to be taken to the same page online.</p>
<p>If you find any problems with this or have any suggestions, please let me know in the comments or email me at chris@ this domain.</p>
]]></content:encoded>
			<wfw:commentRss>http://iamzed.com/2009/04/07/local-to-external-django-docs-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing Django&#039;s Admin Templates and Media When Using the Built-In Development Web Server</title>
		<link>http://iamzed.com/2008/12/11/customizing-django-admin-templates-and-media/</link>
		<comments>http://iamzed.com/2008/12/11/customizing-django-admin-templates-and-media/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 19:12:17 +0000</pubDate>
		<dc:creator>Chris Scott</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://iamzed.com/?p=1266</guid>
		<description><![CDATA[<p>At work today, <a href="http://twitter.com/seanosh">Sean</a> was playing around with creating a Django app and wanted to use the <a href="http://code.google.com/p/django-grappelli/">django-grappelli</a> project to get a cool look for Django’s admin.  I warned him against using the instructions included in the project since if you are&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>At work today, <a href="http://twitter.com/seanosh">Sean</a> was playing around with creating a Django app and wanted to use the <a href="http://code.google.com/p/django-grappelli/">django-grappelli</a> project to get a cool look for Django’s admin.  I warned him against using the instructions included in the project since if you are using Django from an svn checkout then it will break any future svn updates with an error message something like this:</p>
<p><code>working copy does not exist in repository</code></p>
<p>So, we started working on a way around this for a local dev environment.  We knew we could override the admin templates easily but hadn’t really seen anything about how to override the admin media.  Between the two of us, we got it working and figured putting it into writing for future reference would be a good thing.</p>
<h1 id="step_1">Step 1</h1>
<p>First, check out the django-grappelli project. I’m putting mine in the <code>src</code> directory in my home directory:</p>
<div class="dean_ch" style="white-space: wrap;">$ <span class="kw3">cd</span> ~/src<br />
$ <span class="kw3">pwd</span><br />
/Users/chris/src<br />
$ svn checkout http://django-grappelli.googlecode.com/svn/trunk/ django-grappelli</div>
<h1 id="step_2">Step 2</h1>
<p>In your project’s <code>settings.py</code> add a template directory to the templates directory in the django-grappelli directory you created above:</p>
<div class="dean_ch" style="white-space: wrap;">TEMPLATE_DIRS = <span class="br0">&#40;</span><br />
<span class="st0">'/Users/chris/src/django-grappelli/templates'</span>,<br />
<span class="br0">&#41;</span></div>
<p>Also in <code>settings.py</code> change the <code>ADMIN_MEDIA_PREFIX</code> to use the full URL to the server that <code>manage.py</code>’s <code>runserver</code> command will create and append that with a path you want to use. This doesn’t need to relate to any file system path since you’re just going to use it in a urlconf with a static view.</p>
<div class="dean_ch" style="white-space: wrap;">ADMIN_MEDIA_PREFIX = <span class="st0">'http://127.0.0.1:8000/media/admin/'</span></div>
<h1 id="step_3">Step 3</h1>
<p>In your project’s <code>urls.py</code> comment out the lines to enable the admin and add a urlpattern to serve the admin media using the web path you used above from the file system path of the media directory in the django-grappelli project you created in the first step.</p>
<p>In my case, I’m using <code>http://127.0.0.1:8000/media/admin/</code> as my <code>ADMIN_MEDIA_PREFIX</code> which is a web path of <code>/media/admin/</code>.  Since Django strips off the initial slash, your regex to match it would be <code>r'^media/admin/(?P&lt;path&gt;.*)$'</code>. You’ll send these requests to the django static serve view and pass in the <code>document_root</code> which is the file system path to django-grappelli’s <code>media</code> directory.  Your <code>urls.py</code> should now look something like this:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">from</span> django.<span class="me1">conf</span>.<span class="me1">urls</span>.<span class="me1">defaults</span> <span class="kw1">import</span> *</p>
<p><span class="kw1">from</span> django.<span class="me1">contrib</span> <span class="kw1">import</span> admin<br />
admin.<span class="me1">autodiscover</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>urlpatterns = patterns<span class="br0">&#40;</span>”,<br />
<span class="br0">&#40;</span>r<span class="st0">'^admin/(.*)'</span>, admin.<span class="kw3">site</span>.<span class="me1">root</span><span class="br0">&#41;</span>,<br />
<span class="br0">&#40;</span>r<span class="st0">'^media/admin/(?P&lt;path&gt;.*)$'</span>, <span class="st0">'django.views.static.serve'</span>, <span class="br0">&#123;</span><span class="st0">'document_root'</span>: <span class="st0">'/Users/chris/src/django-grappelli/media'</span><span class="br0">&#125;</span><span class="br0">&#41;</span>,<br />
<span class="br0">&#41;</span></div>
<h1 id="finally">Finally</h1>
<p>Now, just do <code>./manage.py runserver</code> in your project directory and browse to <code>http://127.0.0.1:8000/admin/</code> and you should see Grappelli’s sweet styles looking back at you.</p>
<h1 id="notes">Notes</h1>
<p>The key to making this work is using a full URL instead of an absolute or relative URL for your <code>ADMIN_MEDIA_PREFIX</code>. This allows Django to use your normal urlconf to serve up the media with the static serve view.</p>
<p>This is designed to work in a local dev environment using Django’s built-in server.  Don’t use this in production—serve your media using nginx, Apache, etc.</p>
<p>There is probably a better way to do this and suggestions are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://iamzed.com/2008/12/11/customizing-django-admin-templates-and-media/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
