Feb 21 2011

django-meio-easytags released!

Category: Django,Pythonvbmendes @ 01:41

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.

Last friday, my friend Diego Fleury 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’t understand it so did I. So I wanted to make it cleaner and developed the project django-meio-easytags. It’s hosted at Github (http://github.com/vbmendes/django-meio-easytags/).

It’s very easy to create your own template tags with it. Take a look: https://github.com/vbmendes/django-meio-easytags/blob/master/README

Any comments and contributions are welcome.

Tags: ,


Mar 28 2010

get_first_object_or_none shortcut for Django

Category: Django,Pythonvbmendes @ 10:48

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’s very useful when you want to display the last news in the first page of your website, but don’t want it to break if there isn’t one to show. It’s just a use case for such a code, but there are many more.

In the past I used to write something like this:

try:
    first_user = User.objects.all()[:1][0]
except IndexError:
    first_user = None

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’s inspired in the django.shortcuts.get_object_or_404. Let’s take a look at the code:

def get_first_object_or_none(queryset):
    try:
        return queryset[:1][0]
    except IndexError:
        return None

I’ve put this code in a module called shortcuts inside my project and now everytime I want to use it I can write this code:

from shortcuts import get_first_object_or_none
first_user = get_first_object_or_none(User.objects.all())

I think it’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.

Tags: , , ,


Mar 20 2009

Reffering to Django docs from your docs using Sphinx

Category: Djangovbmendes @ 12:25

Sphinx is a great documentation tool, and one of it’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’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 = ['sphinx.ext.intersphinx']
intersphinx_mapping = {'http://docs.python.org/dev': None}

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:

extensions = ['sphinx.ext.intersphinx']
intersphinx_mapping = {
    'http://docs.python.org/dev': None,
    'http://docs.djangoproject.com/en/dev': 'http://docs.djangoproject.com/en/dev/_objects',
}

Sphinx looks for default for the file name objects.inv inside the documentation root, but django doesn’t provide such a file. Since ticket #10315 they provided a file like objects.inv but with a different URL: http://docs.djangoproject.com/en/dev/_objects/.

Now, all you have to do is reffer to classe or any kind of objects inside django: :class:django.contrib.auth.models.User. It will refference the User documentation inside django’s docs.

Tags: , , , ,


Jan 29 2009

Django ORM now supports fields references in filters

Category: Djangovbmendes @ 08:28

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='absolute value')

Will generate somthing like this:

SELECT * FROM table_name WHERE field = 'absolute value';

Now you can filter by another’s table field value.

queryset.filter(field=F('another_field'))

Will generate something like this:

SELECT * FROM table_name WHERE field = another_field;

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.

queryset.filter(views_number__gt=F('comments_number')*2)

Will generate a SQL like this:

SELECT * FROM table_name WHERE views_number = comments_number*2;

It’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.

Tags: , ,


Jan 19 2009

External apps i18n in Django

Category: Djangovbmendes @ 12:59

Django has the good feature of internationalization. It’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 run the command ./manage.py makemessages -l <your-locale>. That’s very easy, but I realized that it don’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’s strings. And, as all in django must be, it is a pluggable way to solve the problem.

All you have to do is create a folder named locale inside your app’s folder and run the command django-admin.py makemessages -l <your-locale> and it will create the file for you. To compile the messages, use the command django-admin.py compilemessages -l <your-locale>

That’s it, as simple as everything in django.

Tags: , , ,


Dec 19 2008

Builtin template tags and filters in Django

Category: Django,Pythonvbmendes @ 12:11

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 a better performance. But there’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 init.py of your project.

from django.template import add_to_builtins
add_to_builtins('path.to.templatetags.file')

Where ‘path.to.templatetags.file’ 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(‘mytagsapp.templatetags.mytags’). My project folder should look like this:

Folders structure

Folders structure

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

Remember to not use this feature with all your template filters as it will mean loss of performance.

Tags: , , , ,


Next Page »