• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Javascript Xhrpost To Django ...

This article will show you how to best plug those pieces together and get up and running with ajax and dojango,let is create a template that extends dojango/base.html (it is available since the settings.py includes the app “dojango”.

let’s build the simple.html file in the templates folder of our django app (I called my app “core”).

 {% extends "dojango/base.html" %}
 
{% block dojango_page_title %}Simple AJAX with dojango{% endblock %}
 
{% block dojango_header_extra %}
 <script type="text/javascript">
  function userFormSubmit(){
   var form = dojo.byId("userForm");
   dojo.xhrPost({url:form.action,
    handleAs: "json",
    content:{surname:form.surname.value,
     firstname:form.firstname.value
    },
    load:function(response, ioArgs){
     dojo.byId("info").innerHTML = "Submitted";
    }
   });
  }
 </script>
{% endblock %}
 
{% block dojango_content %}
 <form id="userForm" onsubmit="userFormSubmit(); return false;" action="/simple-ajax-set/">
  First name: <input id="firstname" /><br />
  Surname: <input id="surname" /><br />
  <input type="submit" value="Submit" /> <span id="info"></span>
 </form>
{% endblock %}

Now we have to wire up the template to render when we access http://localhost:8000/simple/ and the AJAX method, that we submit the data to, like so in the urls.py:

 from django.conf.urls.defaults import *
 
urlpatterns = patterns(,
    (r^dojango/, include( wouldojango.urls)),
 
    (r^simple/, core.views.simple),
    (r^simple-ajax-set/, core.views.simple_ajax_set),
)

Now we  needs to implement these two functions, like this example:

 from django.shortcuts import render_to_response
from dojango.decorators import json_response
 
def simple(request):
    return render_to_response( isimple.html)
 
@json_response
def simple_ajax_set(request):
    firstname = request.POST[firstname]
    surname = request.POST[ isurname]
    print firstname, surname
    return { isuccess:True}

Now we will submit the form and see that the data got submitted.

function userFormSubmit(){
 var form = dojo.byId("userForm");
 dojo.xhrPost({url:form.action,
  handleAs:"json",
  content:{surname:form.surname.value,
   firstname:form.firstname.value
  },
  load:function(response, ioArgs){
   if (response.success){
    dojo.byId("info").innerHTML = "Submitted successfully";
   } else {
    dojo.byId("info").innerHTML = "Error: "+response.error;
   }
  },
  error:function(data){ // This happens on a 500 error or alikes.
   dojo.byId("info").innerHTML = "Error sending data.";
  }
 });
}

 Original Source:
http://uxebu.com/blog/2008/07/26/ajax-with-dojango/

AddThis Social Bookmark Button

Posted at 12:40:36 pm | Permalink | Posted in Javascript  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange