<?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; Python</title>
	<atom:link href="http://www.meiocodigo.com/category/python/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>Sat, 14 Jan 2012 18:44:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>django-meio-easytags 0.4 released!</title>
		<link>http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/</link>
		<comments>http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 04:53:32 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template filters]]></category>
		<category><![CDATA[template tags]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=519</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/' addthis:title='django-meio-easytags 0.4 released! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>I just released the version 0.4 of the django-meio-easytags. I added a subclass of django&#8217;s default template tags Library. And now it&#8217;s easier to create a template tag. Take a look. Projects page Read the Docs Download from Github<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/' addthis:title='django-meio-easytags 0.4 released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/' addthis:title='django-meio-easytags 0.4 released! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>I just released the version 0.4 of the django-meio-easytags. I added a subclass of django&#8217;s default template tags Library. And now it&#8217;s easier to create a template tag. Take a look.</p>

<p><a href="http://meiocodigo.com/projects/django-meio-easytags/">Projects page</a></p>

<p><a href="http://readthedocs.org/docs/django-meio-easytags/en/latest/index.html">Read the Docs</a></p>

<p><a href="https://github.com/vbmendes/django-meio-easytags">Download from Github</a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/' addthis:title='django-meio-easytags 0.4 released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2011/03/02/django-meio-easytags-0-4-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>django-meio-easytags 0.3 released!</title>
		<link>http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/</link>
		<comments>http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/#comments</comments>
		<pubDate>Sun, 27 Feb 2011 02:25:49 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template filters]]></category>
		<category><![CDATA[template tags]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=513</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/' addthis:title='django-meio-easytags 0.3 released! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>I released today the version 0.3 of django-meio-easytags. Now it supports template tags that accepts positional arguments (&#42;args) and keyword arguments (&#42;&#42;kwargs). If you don&#8217;t know how to use this, take a look at Python FAQ. In this release I included some documentation and created a page for the project. Any doubts, suggestions, feature requests, [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/' addthis:title='django-meio-easytags 0.3 released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/' addthis:title='django-meio-easytags 0.3 released! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>I released today the version 0.3 of django-meio-easytags. Now it supports template tags that accepts positional arguments (&#42;args) and keyword arguments (&#42;&#42;kwargs). If you don&#8217;t know how to use this, take a look at <a href="http://docs.python.org/faq/programming#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another">Python FAQ</a>. In this release I included some <a href="http://django-meio-easytags.readthedocs.org/en/latest/index.html">documentation</a> and created a <a href="http://meiocodigo.com/projects/django-meio-easytags/">page for the project</a>.</p>

<p>Any doubts, suggestions, feature requests, feel free to mail me, open an issue at Github or leave a comment.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/' addthis:title='django-meio-easytags 0.3 released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2011/02/26/django-meio-easytags-0-3-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>django-meio-easytags released!</title>
		<link>http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/</link>
		<comments>http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 04:41:03 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=503</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/' addthis:title='django-meio-easytags released! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>django-meio-easytags An easy way to create and parse custom template tags for Django's templating system.<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/' addthis:title='django-meio-easytags released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/' addthis:title='django-meio-easytags released! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>I just released a project I have been working today. A long time ago, I was tired of parsing my template tags by hand, then I created a BaseNode to do this work for me and all I had to do was subclass it and define the method to render the context.</p>

<p>Last friday, my friend <a href="http://www.diegofleury.com.br/">Diego Fleury</a> asked me for some help developing a template tag and I talked to him about my old BaseNode. When I showed the code to him, he didn&#8217;t understand it so did I. So I wanted to make it cleaner and developed the project django-meio-easytags. It&#8217;s hosted at Github (<a href="http://github.com/vbmendes/django-meio-easytags/">http://github.com/vbmendes/django-meio-easytags/</a>).</p>

<p>It&#8217;s very easy to create your own template tags with it. Take a look: <a href="https://github.com/vbmendes/django-meio-easytags/blob/master/README">https://github.com/vbmendes/django-meio-easytags/blob/master/README</a></p>

<p>Any comments and contributions are welcome.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/' addthis:title='django-meio-easytags released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2011/02/21/django-meio-easytags-released/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<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[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/' addthis:title='get_first_object_or_none shortcut for Django '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>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 [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/' addthis:title='get_first_object_or_none shortcut for Django ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/' addthis:title='get_first_object_or_none shortcut for Django '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/' addthis:title='get_first_object_or_none shortcut for Django ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2010/03/28/get_first_object_or_none-shortcut-for-django/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>MXHR, new awesome hot technology!</title>
		<link>http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/</link>
		<comments>http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 23:56:32 +0000</pubDate>
		<dc:creator>fabiomcosta</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=336</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/' addthis:title='MXHR, new awesome hot technology! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>MXHR stands for multiple XmlHttpRequests and was created some days ago by digg.com front end engineers. This awesome technology lets front end engineers request more than one resource in a single XmlHttpRequest. As an example you will be able, in the future (the project is in alpha release), to get a javascript file, a css [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/' addthis:title='MXHR, new awesome hot technology! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/' addthis:title='MXHR, new awesome hot technology! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>MXHR stands for multiple XmlHttpRequests and was created some days ago by digg.com front end engineers.</p>

<p>This awesome technology lets front end engineers request more than one resource in a single XmlHttpRequest.
As an example you will be able, in the future (the project is in alpha release), to get a javascript file, a css and some images with only one request, meaning that you will consume less bandwidth, will get data faster and it will be ordered the way you want. Cool huh?</p>

<p>See more detais on <a title="Digg's blog" href="http://blog.digg.com/?p=621" target="_blank">digg&#8217;s blog.</a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/' addthis:title='MXHR, new awesome hot technology! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/04/23/mxhr-new-awesome-technology/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[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2009/01/15/django-aggregation-released/' addthis:title='Django Aggregation Released '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>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/<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2009/01/15/django-aggregation-released/' addthis:title='Django Aggregation Released ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2009/01/15/django-aggregation-released/' addthis:title='Django Aggregation Released '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2009/01/15/django-aggregation-released/' addthis:title='Django Aggregation Released ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2009/01/15/django-aggregation-released/feed/</wfw:commentRss>
		<slash:comments>2</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[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/' addthis:title='Builtin template tags and filters in Django '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>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 [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/' addthis:title='Builtin template tags and filters in Django ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/' addthis:title='Builtin template tags and filters in Django '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/' addthis:title='Builtin template tags and filters in Django ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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>
		<item>
		<title>Removing Empty Labels from forms.ChoiceField in ModelFormSets</title>
		<link>http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/</link>
		<comments>http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 00:52:00 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[formset]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=260</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/' addthis:title='Removing Empty Labels from forms.ChoiceField in ModelFormSets '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>When you use a Model Choice Field in Django, it automatically creates an empty option in the generated select widget. But in some special cases, you don&#8217;t want this empty option to be there. So I decided to find the way to remove it. It&#8217;s much more simple then you think. Just create your model [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/' addthis:title='Removing Empty Labels from forms.ChoiceField in ModelFormSets ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/' addthis:title='Removing Empty Labels from forms.ChoiceField in ModelFormSets '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>When you use a Model Choice Field in <a href="http://www.djangoproject.com">Django</a>, it automatically creates an empty option in the generated select widget. But in some special cases, you don&#8217;t want this empty option to be there. So I decided to find the way to remove it. It&#8217;s much more simple then you think. Just create your model declaring the model choice field like this:</p>

<p><pre class="brush: python">
fieldname = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)
</pre></p>

<p>It&#8217;s simple like that. Just set the empty_label argument to None. But whe using ModelFormSets, there is a little more work to do. You have to create a new BaseModelFormSet like this:</p>

<p><pre class="brush: python">
from django.forms.models import BaseModelFormSet&lt;/p&gt;

&lt;p&gt;class MyBaseModelFormSet(BaseModelFormSet):
    def add_fields(self, form, index):
        super(MyBaseModelFormSet,self).add_fields(form,index)
        form.fields[&#039;fieldname&#039;] = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)
</pre></p>

<p>Pay atention that you have to override the add_fields method and change your field in the form fields array. Finally you have to pass this BaseModelFormSet as argument to the modelformset_factory:</p>

<p><pre class="brush: python">
from django.forms.models import modelformset_factory&lt;/p&gt;

&lt;p&gt;MyModelFormset = modelformset_factory(MyModel,formset=MyBaseModelFormSet)
</pre></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/' addthis:title='Removing Empty Labels from forms.ChoiceField in ModelFormSets ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

