XP on Rails Extreme Programming Blog

26Apr/090

Ajax update using Django and Prototype

This tutorial will show how to update a list of objects using Ajax and jQuery.

Suppose to have a page that shows all snippets for a given language (for example python).
Then we want to be able to load all ruby snippets without refreshing the page.

Let’s see the html for the “list” page:

<div id="snippetListContainer">
{% for snippet in snippet_list %}
    <div id="snippet-detail-{{snippet.id}}">
        <!-- snippet content here -->
    </div>
{% endfor %}
</div>

Suppose to have a button for the python language and an other for the ruby language.

We want to load all sippets for the currently selected languages: we only need to associate following javascript function on “onclick” event of the button:

<input type="button" id="py-input" onclick="loadSnippets('py');"></input>
<input type="button" id="rb-input" onclick="loadSnippets('rb');"></input>

<script>
function loadSnippets(language)
{
  new Ajax.Updater('snippetListContainer', '/snippets/update_index/'+language, {asynchronous:true});
}
</script>

This javascript function calls asynchronously the server; then it will updates the “snippetListContainer” with the call’s response.

Now let’s see the code for the view:

def update_index(request, language):
    """
    Renders the list of snippets basing on selected button.

    This method is called by an Ajax request that starts every time a user clicks on a lanuage   language button

    Context::
        snippet_list
            the list of snippets to display in the page

    Template::
        myapp/snippets/index_included.html

    """


    snippet_list = db.GqlQuery("SELECT * FROM Snippet WHERE language_code = :1 ORDER BY pub_date DESC", language)
    return render_to_response('myapp/snippets/index_included.html',
                              {'snippet_list' : snippet_list},
                              context_instance=RequestContext(request))

And finally this is the code to place in the “url.py” file:

# Ajax views.
urlpatterns += patterns('',
                        (r'^snippets/update_index/(?P<language>[\w-]+)$', snippets.update_index),
                        )
26Apr/090

Django gravatar tag

This short tutorial shows how to create a gravatar tag in a Django application.

Gravatar is an useful application that lets you to associate an avatar to your email address.
So you can show the same avatar in a large number of web sites without configure it every time.

First of all, we need to write the python code an register the “gravatar” tag:

from django import template

import urllib, hashlib

register = template.Library()

def gravatar(email, size=80):
    gravatar_url = "http://www.gravatar.com/avatar.php?"
    gravatar_url += urllib.urlencode({
        'gravatar_id':hashlib.md5(email).hexdigest(),
        'size':str(size)})
    return """<img src="%s" alt="gravatar for %s" />""" % (gravatar_url, email)

register.simple_tag(gravatar)

Then we can use the new gravatar tag in all templates page, by simply typing:

{% load gravatar %}

{% gravatar user.email 70 %}
Tagged as: , , No Comments