<?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>Meio Código &#187; Django</title>
	<atom:link href="http://www.meiocodigo.com/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.meiocodigo.com</link>
	<description>A peça que faltava para seu código.</description>
	<lastBuildDate>Mon, 19 Jul 2010 19:11:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>get_first_object_or_none shortcut for Django</title>
		<link>http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/</link>
		<comments>http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 13:48:30 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[queryset]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=476</guid>
		<description><![CDATA[I will talk about a shortcut I developed for the Django framework and always use in my apps. This code is aimed in making a quik shortcut to obtain the first object of a queryset if it exists or None otherwise.

It&#8217;s very useful when you want to display the last news in the first page [...]]]></description>
			<content:encoded><![CDATA[<p>I will talk about a shortcut I developed for the <a href="http://www.djangoframework.com/">Django framework</a> and always use in my apps. This code is aimed in making a quik shortcut to obtain the first object of a queryset if it exists or <code>None</code> otherwise.</p>

<p>It&#8217;s very useful when you want to display the last news in the first page of your website, but don&#8217;t want it to break if there isn&#8217;t one to show. It&#8217;s just a use case for such a code, but there are many more.</p>

<p>In the past I used to write something like this:</p>

<p><pre class="brush: python">
try:
    first_user = User.objects.all()[:1][0]
except IndexError:
    first_user = None
</pre></p>

<p>This is four lines to do a very simple task and I wanted to evolve this to use only one line of code. So I developed a function that do this to me. It&#8217;s inspired in the <code>django.shortcuts.get_object_or_404</code>. Let&#8217;s take a look at the code:</p>

<p><pre class="brush: python">
def get_first_object_or_none(queryset):
    try:
        return queryset[:1][0]
    except IndexError:
        return None
</pre></p>

<p>I&#8217;ve put this code in a module called <code>shortcuts</code> inside my project and now everytime I want to use it I can write this code:</p>

<p><pre class="brush: python">
from shortcuts import get_first_object_or_none
first_user = get_first_object_or_none(User.objects.all())
</pre></p>

<p>I think it&#8217;s a very good result and helps a lot in my coding. It also made my code more legible and easy to understand. It increased my productivity and I am publishing to increase others productivity also. Any doubts, questions os suggestions, please leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Arquivos do Mini curso de Django disponibilizados</title>
		<link>http://www.meiocodigo.com/2009/04/26/arquivos-do-mini-curso-de-django-disponibilizados/</link>
		<comments>http://www.meiocodigo.com/2009/04/26/arquivos-do-mini-curso-de-django-disponibilizados/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 02:04:35 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[flisol]]></category>
		<category><![CDATA[mini-curso]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=361</guid>
		<description><![CDATA[No dia 24 de abril de 2009, no SENAC em Natal/RN, ocorreu o FLISOL. Neste evento, eu ministrei um mini curso de django. Então estou disponibilizando tanto os arquivos_mini_curso desenvolvido durante o curso, como os slides.

Mini curso introdutório ao DjangoView more presentations from vbmendes.
]]></description>
			<content:encoded><![CDATA[<p>No dia 24 de abril de 2009, no SENAC em Natal/RN, ocorreu o FLISOL. Neste evento, eu ministrei um mini curso de django. Então estou disponibilizando tanto os <a href="http://www.meiocodigo.com/wp-content/uploads/2009/04/arquivos_mini_curso.zip">arquivos_mini_curso</a> desenvolvido durante o curso, como os <a href="http://www.slideshare.net/vbmendes/mini-curso-introdutrio-ao-django-1345709">slides</a>.</p>

<div style="width:425px;text-align:left" id="__ss_1345709"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/vbmendes/mini-curso-introdutrio-ao-django-1345709?type=powerpoint" title="Mini curso introdutório ao Django">Mini curso introdutório ao Django</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mini-curso-slides-090426210203-phpapp01&#038;stripped_title=mini-curso-introdutrio-ao-django-1345709" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mini-curso-slides-090426210203-phpapp01&#038;stripped_title=mini-curso-introdutrio-ao-django-1345709" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object><div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/vbmendes">vbmendes</a>.</div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/04/26/arquivos-do-mini-curso-de-django-disponibilizados/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Festival Latino-americano de Instalação de Software Livre</title>
		<link>http://www.meiocodigo.com/2009/04/24/festival-latino-americano-de-instalacao-de-software-livre/</link>
		<comments>http://www.meiocodigo.com/2009/04/24/festival-latino-americano-de-instalacao-de-software-livre/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 21:58:36 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[flisol]]></category>
		<category><![CDATA[minicurso]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=343</guid>
		<description><![CDATA[

Venho através deste convidá-los ao FLISOL/RN 2009. O Festival Latino-americano de Instalação de Software Livre do RN irá acontecer no dia 25 de abril de 2009 no SENAC em Natal.

No evento eu irei ministrar um mini-curso introdutório ao framework Django das 14:00 as 17:00 horas, cujo objetivo é formar pessoas com conhecimentos básicos acerca do [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://rn.softwarelivre.org/flisol/2009/"><img class="alignnone size-full wp-image-342" title="flisol" src="http://www.meiocodigo.com/wp-content/uploads/2009/04/flisol.png" alt="flisol" width="441" height="207" /></a></p>

<p>Venho através deste convidá-los ao <a href="http://rn.softwarelivre.org/flisol/2009/">FLISOL/RN 2009</a>. O Festival Latino-americano de Instalação de Software Livre do RN irá acontecer no dia 25 de abril de 2009 no SENAC em Natal.</p>

<p>No evento eu irei ministrar um mini-curso introdutório ao framework Django das 14:00 as 17:00 horas, cujo objetivo é formar pessoas com conhecimentos básicos acerca do framework. O mini-curso irá abordar diversos conceitos do django através do desenvolvimento de um blog.</p>

<p>Maiores informações sobre o evento podem ser encontradas no <a href="http://rn.softwarelivre.org/flisol/2009/">site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/04/24/festival-latino-americano-de-instalacao-de-software-livre/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reffering to Django docs from your docs using Sphinx</title>
		<link>http://www.meiocodigo.com/2009/03/20/reffering-to-django-docs-from-your-docs-using-sphinx/</link>
		<comments>http://www.meiocodigo.com/2009/03/20/reffering-to-django-docs-from-your-docs-using-sphinx/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 15:25:49 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[docs]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=322</guid>
		<description><![CDATA[Sphinx is a great documentation tool, and one of it&#8217;s built-in extensions is sphinx.ext.intersphinx. It simply takes your objects references for other projects and converts to links in the other project&#8217;s documentation.

In sphinx-quickstart command, it asks if you want to add this extension. If so, It adds this code to your conf.py file:


extensions = [&#039;sphinx.ext.intersphinx&#039;]
intersphinx_mapping [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sphinx.pocoo.org/">Sphinx</a> is a great documentation tool, and one of it&#8217;s built-in extensions is <a href="http://sphinx.pocoo.org/latest/ext/intersphinx.html">sphinx.ext.intersphinx</a>. It simply takes your objects references for other projects and converts to links in the other project&#8217;s documentation.</p>

<p>In <code>sphinx-quickstart</code> command, it asks if you want to add this extension. If so, It adds this code to your conf.py file:</p>

<p><pre class="brush: python">
extensions = [&#039;sphinx.ext.intersphinx&#039;]
intersphinx_mapping = {&#039;http://docs.python.org/dev&#039;: None}
</pre></p>

<p>As you can see, it comes integrated with python documentation by default. But what I want is to integrate with Django documentation. So I changed the code a little bit:</p>

<p><pre class="brush: python">
extensions = [&#039;sphinx.ext.intersphinx&#039;]
intersphinx_mapping = {
    &#039;http://docs.python.org/dev&#039;: None,
    &#039;http://docs.djangoproject.com/en/dev&#039;: &#039;http://docs.djangoproject.com/en/dev/_objects&#039;,
}
</pre></p>

<p>Sphinx looks for default for the file name objects.inv inside the documentation root, but django doesn&#8217;t provide such a file. Since <a href="http://code.djangoproject.com/ticket/10315">ticket #10315</a> they provided a file like objects.inv but with a different URL: http://docs.djangoproject.com/en/dev/_objects/.</p>

<p>Now, all you have to do is reffer to classe or any kind of objects inside django: <code>:class:<code>django.contrib.auth.models.User</code></code>. It will refference the User documentation inside django&#8217;s docs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/03/20/reffering-to-django-docs-from-your-docs-using-sphinx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Documenting Django pluggable apps with Sphinx</title>
		<link>http://www.meiocodigo.com/2009/03/18/documenting-django-pluggable-apps-with-sphinx/</link>
		<comments>http://www.meiocodigo.com/2009/03/18/documenting-django-pluggable-apps-with-sphinx/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 18:06:46 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[docs]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[pluggable apps]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=317</guid>
		<description><![CDATA[I decided to document my Django projects with the same tool used by Django team: Sphinx. It is a very good documenting tool, and was used for many other projects, including the Python Project itself.

But it has a simple problem with Django, that I found the solution rlazo&#8217;s blog. But it still has some problems, [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to document my Django projects with the same tool used by Django team: <a href="http://sphinx.pocoo.org/">Sphinx</a>. It is a very good documenting tool, and was used for many other projects, including the Python Project itself.</p>

<p>But it has a simple problem with Django, that I found the solution <a href="http://www.rlazo.org/blog/entry/2008/oct/24/documenting-a-django-project-with-sphinx/">rlazo&#8217;s blog</a>. But it still has some problems, when you are trying to document a pluggable app, that doesn&#8217;t have a project, you have to configure the django settings without the project settings, since pluggable apps doesn&#8217;t have a settings module.</p>

<p>Thanks to apollo13, who shown me this better approach, you can simply add this code to sphinx&#8217;s conf.py file:</p>

<p><pre class="brush: python">
from django.conf import settings
settings.configure()
</pre></p>

<p>With this approach, you don&#8217;t need to add any kind of fake files in your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/03/18/documenting-django-pluggable-apps-with-sphinx/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MEDIA_URL and context from processors available in HTTP 500 template</title>
		<link>http://www.meiocodigo.com/2009/02/01/media_url-and-context-from-processors-available-in-http-500-template/</link>
		<comments>http://www.meiocodigo.com/2009/02/01/media_url-and-context-from-processors-available-in-http-500-template/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 01:43:58 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[media_url]]></category>
		<category><![CDATA[template context processor]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=285</guid>
		<description><![CDATA[It&#8217;s easy to create a custom template for HTTP 500 errors. All you have to do is create a file named 500.html in any of the application&#8217;s TEMPLATE_DIRS. But in almost all cases you want to use media in this page. It&#8217;s good to have an error page with the same look and feel of [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s easy to create a custom template for HTTP 500 errors. All you have to do is create a file named 500.html in any of the application&#8217;s TEMPLATE_DIRS. But in almost all cases you want to use media in this page. It&#8217;s good to have an error page with the same look and feel of the system itself.</p>

<p>The default HTTP 500 handler in django uses Context instead of RequestContext. It means that none of your template context processors will be loaded, and the MEDIA_URL variable available in the template system comes exactly from one of these processors. So, without template context processors, we get no MEDIA_URL variable in templates.</p>

<p>Searching in google for a solution to this problem I found a snippet[1] that solves the MEDIA_URL problem. But it still doesn&#8217;t load the processors, all it does is pass the MEDIA_URL variable to the context for template. So I decided to make simple changes in it to load the processors. And here is the complete code:</p>

<p><pre class="brush: python">
def server_error(request, template_name=&#039;500.html&#039;):
    from django.template import RequestContext
    from django.http import HttpResponseServerError
    t = loader.get_template(template_name)
    return HttpResponseServerError(t.render(RequestContext(request)))
</pre></p>

<p>Put this code in any views file you want and then define it as the handler500 in your urls.py:</p>

<p><pre class="brush: python">
handler500 = &#039;views.server_error&#039;
</pre></p>

<p>[1] <a href="http://www.djangosnippets.org/snippets/1199/">http://www.djangosnippets.org/snippets/1199/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/02/01/media_url-and-context-from-processors-available-in-http-500-template/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django ORM now supports fields references in filters</title>
		<link>http://www.meiocodigo.com/2009/01/29/django-orm-now-supports-fields-references-in-filters/</link>
		<comments>http://www.meiocodigo.com/2009/01/29/django-orm-now-supports-fields-references-in-filters/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 11:28:50 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=281</guid>
		<description><![CDATA[Today was added a new feature to django ORM. Now it supports references to fields in filters expressions. Just checkout the SVN version and enjoy it.

Before this update, queryset could be filtered only by absolute values.


queryset.filter(field=&#039;absolute value&#039;)


Will generate somthing like this:


SELECT * FROM table_name WHERE field = &#039;absolute value&#039;;


Now you can filter by another&#8217;s table [...]]]></description>
			<content:encoded><![CDATA[<p>Today was added a new feature to django ORM. Now it supports references to fields in filters expressions. Just checkout the SVN version and enjoy it.</p>

<p>Before this update, queryset could be filtered only by absolute values.</p>

<p><pre class="brush: python">
queryset.filter(field=&#039;absolute value&#039;)
</pre></p>

<p>Will generate somthing like this:</p>

<p><pre class="brush: sql">
SELECT * FROM table_name WHERE field = &#039;absolute value&#039;;
</pre></p>

<p>Now you can filter by another&#8217;s table field value.</p>

<p><pre class="brush: python">
queryset.filter(field=F(&#039;another_field&#039;))
</pre></p>

<p>Will generate something like this:</p>

<p><pre class="brush: sql">
SELECT * FROM table_name WHERE field = another_field;
</pre></p>

<p>The feature also supports arithmetical operations in the lookups so you can do filters where the views_number is twice as big as the comments_number.</p>

<p><pre class="brush: python">
queryset.filter(views_number__gt=F(&#039;comments_number&#039;)*2)
</pre></p>

<p>Will generate a SQL like this:</p>

<p><pre class="brush: sql">
SELECT * FROM table_name WHERE views_number = comments_number*2;
</pre></p>

<p>It&#8217;s a very good feature. So, checkout the SVN version and take a look at docs/topics/db/queries.txt to know more about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/01/29/django-orm-now-supports-fields-references-in-filters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>External apps i18n in Django</title>
		<link>http://www.meiocodigo.com/2009/01/19/external-apps-i18n-in-django/</link>
		<comments>http://www.meiocodigo.com/2009/01/19/external-apps-i18n-in-django/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 15:59:21 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=270</guid>
		<description><![CDATA[Django has the good feature of internationalization. It&#8217;s very simple to use that it generates the .po files automatically from your project. All you have to do is complete the .po file with the translated text. To create these files, all you have to do is create a folder in your project called locale and [...]]]></description>
			<content:encoded><![CDATA[<p>Django has the good feature of internationalization. It&#8217;s very simple to use that it generates the <code>.po</code> files automatically from your project. All you have to do is complete the <code>.po</code> file with the translated text. To create these files, all you have to do is create a folder in your project called <code>locale</code> and run the command <code>./manage.py makemessages -l &lt;your-locale&gt;</code>. That&#8217;s very easy, but I realized that it don&#8217;t get the string inside your external apps. So, after some search in internet, i found a way to get this file with the app&#8217;s strings. And, as all in django must be, it is a pluggable way to solve the problem.</p>

<p>All you have to do is create a folder named <code>locale</code> inside your app&#8217;s folder and run the command <code>django-admin.py makemessages -l &lt;your-locale&gt;</code> and it will create the file for you. To compile the messages, use the command <code>django-admin.py compilemessages -l &lt;your-locale&gt;</code></p>

<p>That&#8217;s it, as simple as everything in django.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/01/19/external-apps-i18n-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django Aggregation Released</title>
		<link>http://www.meiocodigo.com/2009/01/15/django-aggregation-released/</link>
		<comments>http://www.meiocodigo.com/2009/01/15/django-aggregation-released/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 14:57:51 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[aggregation]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=268</guid>
		<description><![CDATA[The aggregation is already available in Django SVN version. Now, it&#8217;s possible to do things such count authors of all book entries, count comments os all post entries, sum values of all sales to a client and much more.

Here is the official documentation of this feature: http://docs.djangoproject.com/en/dev/topics/db/aggregation/
]]></description>
			<content:encoded><![CDATA[<p>The aggregation is already available in Django SVN version. Now, it&#8217;s possible to do things such count authors of all book entries, count comments os all post entries, sum values of all sales to a client and much more.</p>

<p>Here is the official documentation of this feature: <a href="http://docs.djangoproject.com/en/dev/topics/db/aggregation/">http://docs.djangoproject.com/en/dev/topics/db/aggregation/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/01/15/django-aggregation-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Builtin template tags and filters in Django</title>
		<link>http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/</link>
		<comments>http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 15:11:39 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[builtins]]></category>
		<category><![CDATA[template filters]]></category>
		<category><![CDATA[template tags]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=264</guid>
		<description><![CDATA[A good feature of Django Framework is the template tags and template filters. But it sucks when you have to load the filters in each template like this:


{% load mytemplatetags %}


It was much better if you just use the filters, without the need to load it. The reason you have to load is to achieve [...]]]></description>
			<content:encoded><![CDATA[<p>A good feature of <a href="http://www.djangoproject.com">Django Framework</a> is the template tags and template filters. But it sucks when you have to load the filters in each template like this:</p>

<p><pre class="brush: python">
{% load mytemplatetags %}
</pre></p>

<p>It was much better if you just use the filters, without the need to load it. The reason you have to load is to achieve a better performance. But there&#8217;s a function of the template module called add_to_builtins that solves this problem. You can easily define a template tag or filter as builtin and use it like the builtin django template tags and filters. Just put this code in a file that is loaded ever, like the <strong>init</strong>.py of your project.</p>

<p><pre class="brush: python">
from django.template import add_to_builtins
add_to_builtins(&#039;path.to.templatetags.file&#039;)
</pre></p>

<p>Where &#8216;path.to.templatetags.file&#8217; is the path of the file containing the template tags. For example, I have an app, inside my project, named mytagsapp. Inside this app I have the module templatetags with a file named mytags.py with my custom tags. So I will have to call add_builtins(&#8216;mytagsapp.templatetags.mytags&#8217;). My project folder should look like this:</p>

<div id="attachment_265" class="wp-caption alignleft" style="width: 230px"><a href="http://www.meiocodigo.com/wp-content/uploads/2008/12/blog-folders.png"><img src="http://www.meiocodigo.com/wp-content/uploads/2008/12/blog-folders.png" alt="Folders structure" title="blog-folders" width="220" height="300" class="size-full wp-image-265" /></a><p class="wp-caption-text">Folders structure</p></div>

<p>Make sure your app is in the INSTALED_APPS setting in your settings.py project. It will guarantee that the <strong>init</strong>.py of you app will get called. So just put the code above in this file (red in image).</p>

<p>Remember to not use this feature with all your template filters as it will mean loss of performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.254 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-09-02 19:46:25 -->
