In last post, a quick-and-dirty prototype is developed to make Gelman running. In this post, I would tail the XSLT for our needs.

If you have not tried XSLT, consider the XSLT (for dummies and official reference). It is a powerful tool, if you only take interest data extraction, You could compare the efforts to parse XML, for example, PyAWS. The xsl is not in the repository since we need a URI to access.

Let’s keep it simpler and more stupid. Since AWS support both ISBN query and keyword search, we could combine the two input box as one and send different requests to AWS based on the knowledge of ISBN validation. Check r12 for the implementation, here is the screenshot:

Add book by search



In r14, we decided to take a try of dojo.query, an alternative of jQuery; it is really cool to reference the object using CSS syntax.

dojo.forEach(dojo.query("#book_form input[@class=incoming]"), function(item) {
                               if (item.value != "") {
                                       formdata.push(cache[i]);
                               }
                               i++;
                       });

In the above code snippet, cache holds the JSON objects parsed from AWS request, if an eBook file is attached to incoming(upload does not work now, just use it as the mark), the corresponding meta data is collected in formdata. The last question is how to POST the it to the server?

JSON is supported in both client and server sides, i.e dojo.json and django.utils.simplejson. In client side, we could serialize the JavaScript to JSON string:

dojo.xhrPost({
                url: dojo.byId("book_form").action,
                content: { items: dojo.toJson(formdata) },
            }).addCallback(function(response) {

And in server side, just loads the JSON string to Python objects. Check r16 for implementation.

items = simplejson.loads(request.POST[‘items’])

Next section, we would discuss how to manipulate the database using Django’s database API.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

6 Comments to “Learning Django by Example(2): Show me your data”

  1. Learning Django by Example(1): Start the Engine by Refactor the Life | September 5th, 2007 at 6:04 am

    [...] NOTE: All the parameters are case-sensitive, especially CallBack for callbackParamName. Check r10. Oops, I forgot add it to the repository, so please check r11 for the updated version, further explanation is in section 2. [...]

  2. Learning Django by Example(3): Just works by Refactor the Life | September 8th, 2007 at 2:11 pm

    [...] Learning Django by Example(3): Just works Posted on September 7th, 2007 in Web In the last post, we succeeded to post the Javascript objects via JSON string to the server side, the data is to be [...]

  3. Karl TiedtNo Gravatar | October 10th, 2007 at 6:41 am

    Just leaving my footprint in the sand. Found your latest blog on this and reading the entire series.

    Just wanted to piggy back on Alex’s comment from part 1 about forEach and recommend a change.

    dojo.query(“#book_form input[@class=incoming]”).forEach(function(item) {
    if (item.value != “”) {
    formdata.push(cache[i]);
    }
    i++;
    });

    Slightly simpler code :)

    Keep up the great articles!
    -Karl

  4. tomcaskNo Gravatar | February 11th, 2008 at 12:27 pm

    New Translation!! ;)

    http://tomascasquero.com/javascript/pami-aprendiendo-django-con-ejemplos2-muestrame-informacion/

  5. Refactor the Life » Blog Archive » PyAWS 0.3.0 released | May 6th, 2008 at 10:19 pm

    [...] almost abandoned this project as I found the XSLT approach is more appealing: ideal for AJAX application and easy to integrate via simplejson in the server [...]

  6. Getting Started With… | Resource Pile | July 16th, 2008 at 1:48 pm

    [...] Web Monkey’s Getting Started with Django Kunxi.org’s Learning Django by Example(Part 1, 2, 3, 4, 5, 6, 7, [...]

Leave a Comment

This site is using OpenAvatar based on