I’ve been doing a lot Django reading recently (the docs, tutorials, the book, etc) for my current job (not launched yet). I’ve had to dive into the code a few times and it’s been a really fun ride. To help others “get up and running” as soon as possible, I thought I’d post some of the interesting tidbits I learnt from the django book. Ofcourse, you’ll learn more by reading the book yourself and diving into the source code head on.

Here are some cool tricks from the first few chapters, with more coming in the next few days:

  1. Instead of printing values of variables to help with debugging, just put “assert False” in your view and you’ll drop to the error page where you can view all your current variables.

    Ofcourse, if you have ipython installed (and there is no reason you shouldn’t, google it now if you don’t know about it), you can put the following in your view at the top:

    
    from IPython.Shell import IPShellEmbed
    ipython = IPShellEmbed()
    
     

    Now, whenever you put ipython() in your view, you’ll drop into an ipython shell as soon as django’s server hits that view.

  2. By default whenever you do the following in a template

    {{ some_model.some_method_or_attr }}
    

    django will do a dictionary, attribute, method or list index lookup (in that order). You really don’t want to end up in a situation where “some_method_or_var” is a method that can destructively affect your model. There’s a simple way to fix this:

    
    def some_destructive_method(self):
        pass
    
    some_destructive_method.alters_data = True
    
    

    Now, the template system will never execute the method call from the template (it’ll fail silently).

  3. Think its annoying to declare variables in your view and then pass them all one by one to your template like so:

    def my_view(request):
        var1= .. do something
        var2= .. do something
        varn= .. do something
        return render_to_response("template.html", {
            'var1':var1,'var2':var2..,'varn':varn
        }, context_instance=RequestContext(request))
    

    instead, you can use the locals() function (which returns a dict of all local variables for you to conveniently pass it all to the template. Not very pythonic (be explicit), and I personally would prefer never to use it, but a nice shortcut.

  4. You can do {{ block.super }} within a block in a template to get the rendered content of the parent template. A simplistic use-case of this would be using it for the title of the page:

    # base.html
    {% block title %} My cool site {% endblock title %}
    
    # child.html
    {% extends "base.html" %}
    {% block title %} {{ block.super }} : The child page {% endblock title %}
    
  5. Always find yourself getting data from your model and sorting it by a certain field? Put the default sort field in your model like so:

    class News:
        class Meta:
            ordering=['date_updated']
    
  6. You can use index slicing on QuerySet objects too (which translate to sql LIMIT statements). So, get your last 10 posts by:

    Posts.objects.all()[0:10]
    

These are some of the simpler/easier tricks. Some of the more complex stuff’ll be up next..

Trackback

only 1 comment

  1. One of the greatest benefits of Django is its kind and helpful user community. For help with any aspect of Django from installation, to application design, to database design, to deployment feel free to ask questions online.

Add your comment