<?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>Mike Chambers &#187; django</title>
	<atom:link href="http://www.mikechambers.com/blog/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog</link>
	<description>code = joy</description>
	<lastBuildDate>Thu, 29 Jul 2010 18:12:24 +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>Creating a default DATETIME_FORMAT filter for django</title>
		<link>http://www.mikechambers.com/blog/2008/05/02/creating-a-default-datetime_format-filter-for-django/</link>
		<comments>http://www.mikechambers.com/blog/2008/05/02/creating-a-default-datetime_format-filter-for-django/#comments</comments>
		<pubDate>Fri, 02 May 2008 23:46:41 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1238</guid>
		<description><![CDATA[Here is a simple django template filter that will format a date according to the DATETIME_FORMAT variable in the settings file. from django import template from settings import DATETIME_FORMAT from django.template.defaultfilters import date register = template.Library() @register.filter def default_datetime(value): try: v = date(value, DATETIME_FORMAT) except: return value return v Save this in a file named [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F05%2F02%2Fcreating-a-default-datetime_format-filter-for-django%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F05%2F02%2Fcreating-a-default-datetime_format-filter-for-django%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Here is a simple <a href="http://www.djangoproject.com">django</a> <a href="http://www.djangoproject.com/documentation/templates/">template</a> <a href="http://www.djangoproject.com/documentation/templates/#built-in-filter-reference">filter</a> that will format a date according to the <a href="http://www.djangoproject.com/documentation/settings/#datetime-format">DATETIME_FORMAT</a> variable in the settings file.<br />
<span id="more-1238"></span></p>
<div class="highlight">
<pre><code><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">django</span> <span style="color: #008000; font-weight: bold">import</span> template
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">settings</span> <span style="color: #008000; font-weight: bold">import</span> DATETIME_FORMAT
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">django.template.defaultfilters</span> <span style="color: #008000; font-weight: bold">import</span> date

register <span style="color: #666666">=</span> template<span style="color: #666666">.</span>Library()

<span style="color: #AA22FF">@register</span><span style="color: #666666">.</span>filter
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">default_datetime</span>(value):
	<span style="color: #008000; font-weight: bold">try</span>:
		v <span style="color: #666666">=</span> date(value, DATETIME_FORMAT)
	<span style="color: #008000; font-weight: bold">except</span>:
		<span style="color: #008000; font-weight: bold">return</span> value

	<span style="color: #008000; font-weight: bold">return</span> v
</pre>
<p></code></div>
<p>Save this in a file named <em>default_date_filters.py</em> and place it in a directory called <em>templatetags</em> in your application directory (along with a file named <em>__init__.py</em>.</p>
<p>You can then use the filter in a template like so:</p>
<pre><code>
{% load default_date_filters  %}

{% item.created_date|default_datetime%}
</code></pre>
<p>&nbsp;</p>
<p>You can find more information and how to create and use custom template filters in django <a href="http://www.biais.org/blog/index.php/2007/01/11/11-django-custom-template-filters">here</a> and <a href="http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-filters">here</a>. You can find information on django settings <a href="http://www.djangoproject.com/documentation/settings/#datetime-format">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/05/02/creating-a-default-datetime_format-filter-for-django/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
