<?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>Baking Noodles &#187; django</title>
	<atom:link href="http://bakingnoodles.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://bakingnoodles.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 26 May 2010 14:18:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fixing Firefox form caching in Django</title>
		<link>http://bakingnoodles.com/2010/05/fixing-firefox-form-caching-in-django/</link>
		<comments>http://bakingnoodles.com/2010/05/fixing-firefox-form-caching-in-django/#comments</comments>
		<pubDate>Wed, 26 May 2010 14:16:57 +0000</pubDate>
		<dc:creator>Simon Oxley</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://bakingnoodles.com/?p=188</guid>
		<description><![CDATA[I noticed recently that Firefox seems to cache form fields very aggressively, particularly tickbox values. This can result in the cached values being displayed instead of the updated content from the server. It usually happens when the submitted form returns to the same URL &#8211; e.g. an Update Profile page &#8211; giving the impression that [...]]]></description>
			<content:encoded><![CDATA[<p>I noticed recently that Firefox seems to cache form fields very aggressively, particularly tickbox values. This can result in the cached values being displayed instead of the updated content from the server. It usually happens when the submitted form returns to the same URL &#8211; e.g. an Update Profile page &#8211; giving the impression that changes haven&#8217;t been saved.  Bad Firefox!  A re-direct after POST doesn&#8217;t seem to resolve the issue.</p>
<p>The only way to force Firefox to display the updated content is to set the &#8216;no-cache&#8217; HTTP header. Fortunately Django has a cache framework that can be used out-of-the-box to do this individually for each view. The key piece is the @cache_control decorator.</p>
<p>The solution very simple, just import the decorator and add it to the view that handles the form processing. The full docs are on the <a href="http://docs.djangoproject.com/en/dev/topics/cache/#controlling-cache-using-other-headers" target="_blank">Django site</a>. I&#8217;ve included a short example below.</p>
<pre class="brush: python">

from django.views.decorators.cache import cache_control

@cache_control(no_cache=True)
def my_form_view(request):
...rest of view
</pre>
<p>The result can be confirmed by viewing the http response headers, the <a href="https://addons.mozilla.org/en-US/firefox/addon/60/" target="_blank">Firefox Web Developer extension</a> is a handy tool for this. If everything is working you&#8217;ll see:</p>
<pre>Cache-Control: no-cache</pre>
<p>Job Done.  Thanks Django!</p>
]]></content:encoded>
			<wfw:commentRss>http://bakingnoodles.com/2010/05/fixing-firefox-form-caching-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crowdsourcing Prague for EuroDjangoCon 2009</title>
		<link>http://bakingnoodles.com/2009/04/crowdsourcing-prague-for-eurodjangocon-2009/</link>
		<comments>http://bakingnoodles.com/2009/04/crowdsourcing-prague-for-eurodjangocon-2009/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 13:29:13 +0000</pubDate>
		<dc:creator>Simon Oxley</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://bakingnoodles.com/?p=142</guid>
		<description><![CDATA[

I&#8217;m off to Prague tomorrow ahead of next week&#8217;s EuroDjangoCon. 
In a great demonstration of crowdsourcing, jacobian has created a Google Map for Prague which is now being populated by conference goers.
View EuroDjangoCon in a larger map
So far it includes the conference hotel, tram stops and a bunch of bars, restaurants and sites. Who needs [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Official EuroDjangoCon site." href="http://euro.djangocon.org"><br />
<img style="border: 0pt none; margin: 0px;" src="http://euro.djangocon.org/site_media/images/attending.png" border="0" alt="Attending EuroDjangoCon" width="136" height="71" /></a></p>
<p>I&#8217;m off to Prague tomorrow ahead of next week&#8217;s <a href="http://euro.djangocon.org/">EuroDjangoCon</a>. </p>
<p>In a great demonstration of <a href="http://en.wikipedia.org/wiki/Crowdsourcing">crowdsourcing</a>, <a href="http://jacobian.org/">jacobian </a>has created a <a href="http://maps.google.com/maps/ms?ie=UTF&amp;msa=0&amp;msid=105729400191768797417.00046815a0bc660d9cba2">Google Map for Prague </a>which is now being populated by conference goers.</p>
<p><iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?ie=UTF8&amp;msa=0&amp;msid=105729400191768797417.00046815a0bc660d9cba2&amp;z=12&amp;output=embed"></iframe><br /><small>View <a href="http://maps.google.com/maps/ms?ie=UTF8&amp;msa=0&amp;msid=105729400191768797417.00046815a0bc660d9cba2&amp;z=12&amp;source=embed" style="color:#0000FF;text-align:left">EuroDjangoCon</a> in a larger map</small></p>
<p>So far it includes the conference hotel, tram stops and a bunch of bars, restaurants and sites. Who needs a guidebook!</p>
<p><a href="http://euro.djangocon.org/">EuroDjangoCon </a>has been organized by <a href="http://www.siudesign.co.uk/">Robert Lofthouse</a> and takes place in Prague on 4th-6th of May 2009 (with development sprints on the 7th and 8th).</p>
<p>I&#8217;m looking forward to meeting up with fellow Django folks so please stop me and say Hi if you see me wandering around.</p>
]]></content:encoded>
			<wfw:commentRss>http://bakingnoodles.com/2009/04/crowdsourcing-prague-for-eurodjangocon-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Command Line Email with Django and Gmail</title>
		<link>http://bakingnoodles.com/2009/03/command-line-email-with-django-and-gmail/</link>
		<comments>http://bakingnoodles.com/2009/03/command-line-email-with-django-and-gmail/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 12:55:48 +0000</pubDate>
		<dc:creator>Simon Oxley</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://bakingnoodles.com/?p=35</guid>
		<description><![CDATA[I needed to find a way to send email reports from a scheduled cron job. I looked for a simple linux command-line email solution but couldn&#8217;t find anything that fitted the bill. I was already using Django&#8217;s EmailMessage class in an existing app, so I started to think about how I could re-use it for [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to find a way to send email reports from a scheduled cron job. I looked for a simple linux command-line email solution but couldn&#8217;t find anything that fitted the bill. I was already using <a href="http://docs.djangoproject.com/en/dev/topics/email/#the-emailmessage-and-smtpconnection-classes">Django&#8217;s EmailMessage</a> class in an existing app, so I started to think about how I could re-use it for scheduled jobs.</p>
<p>I&#8217;m using linux in this scenario, but it would probably work fine in Windows too.</p>
<p>First I configured my cron job (actually the .sh script that cron job runs) to output to a file called <code>report.txt</code>. This would be overwritten each time the job ran, so would always be the latest version.</p>
<p>An existing Django project <code>settings.py</code> had the following settings to enable email sending via a Gmail (or Google Apps) account. The settings are based on Nathan Ostgard&#8217;s <a href="http://nathanostgard.com/archives/2007/7/2/gmail_and_django/">Gmail and Django</a> article.<a href="http://nathanostgard.com/archives/2007/7/2/gmail_and_django/"><br />
</a></p>
<pre class="brush: python">

EMAIL_HOST = &#039;smtp.gmail.com&#039;
EMAIL_HOST_USER = &#039;user@domain.com&#039;
EMAIL_HOST_PASSWORD = &#039;password&#039;
EMAIL_PORT = 587
EMAIL_USE_TLS = True
</pre>
<p>So I knocked up a standalone python script <code>sendreport.py</code> that would take these settings and email the <code>report.txt</code> file to me.</p>
<pre class="brush: python">
#!/user/bin/python
#
# Get settings from an existing django project
import sys
import os
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), &#039;/..&#039;))
os.environ[&#039;DJANGO_SETTINGS_MODULE&#039;] = &#039;my_django_project.settings&#039;

from django.core.mail import EmailMessage

# Set up email fields
recipient = &#039;myemail@mydomain.com&#039;
subject = &#039;CRON Job Report&#039;
body = &#039;CRON Job Report for blah, blah, blah . . .&#039;

# Create email object, attach file, send
email = EmailMessage(subject, body, to = [recipient])
email.attach_file(&#039;report.txt&#039;)
email.send()
</pre>
<p>The <code>EmailMessage </code>class makes it very easy to attach a file and send the email.  I can tag this on to the end of the .sh script executed by the cron job and jobs-a-good-un <img src='http://bakingnoodles.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Well sort of . .  it&#8217;s functional, but not ideal. It works by reading email settings from an existing Django app&#8217;s <code>setting.py</code>. I wanted it to be more portable, and not dependent on a specific Django configuration/app.</p>
<p>Then I found out that you can specify Django settings from within a standalone python script. Could this method be used to break any dependencies and make the script more portable? Absolutely!</p>
<pre class="brush: python">
#!/user/bin/python
#
# Configure some standalone django settings
# These settings are for a gmail or google apps account
from django.conf import settings
settings.configure (
EMAIL_HOST = &#039;smtp.gmail.com&#039;
EMAIL_HOST_USER = &#039;user@domain.com&#039;
EMAIL_HOST_PASSWORD = &#039;password&#039;
EMAIL_PORT = 587
EMAIL_USE_TLS = True
)

from django.core.mail import EmailMessage
import datetime
import sys
import socket

# Get the hostname and current time to put in the email
hostname = socket.gethostname()
timestamp = datetime.datetime.now().ctime()

# Set up email fields
recipient = &#039;myemail@mydomain.com&#039;
subject = &#039;%s CRON Job Report: %s&#039; % (hostname, timestamp)
body = &#039;Report for %s on %s attached&#039; % (hostname, timestamp)

# Create email object, attach file, send
email = EmailMessage(subject, body, to = [recipient])
email.attach_file(&#039;bkp_report.txt&#039;)
email.send()
</pre>
<p>I also made use of <code>hostname()</code>and <code>datetime()</code> to provide some more detail for the email subject and body.</p>
<p>Now the only dependency is Django itself &#8211; which I&#8217;m happy with, my job reports are archived in email &#8211; which I like, and I get to use more Python &#8211; which is good for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://bakingnoodles.com/2009/03/command-line-email-with-django-and-gmail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
