- Author:
- heckj
- Posted:
- June 21, 2007
- Language:
- JavaScript
- Version:
- Not specified
- Score:
- 5 (after 5 ratings)
An example of using Dojo to retrieve some information from a view (linked by the URL '/account/isavailable/') to show whether or not an account is available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <script language="JavaScript" type="text/javascript">
dojo.require("dojo.io.*");
dojo.require("dojo.json");
function sendFormCallback(type, data, evt) {
if (type == 'error')
alert('Error when retrieving data from the server!');
else
arrayData = dojo.json.evalJson(data);
// returned data from this critter will be an array with two
// keys - "username" and "available". i.e.
// {"username": "asdg", "available": true}
if (arrayData["available"])
dojo.byId("check_available_results").innerHTML = "Available"
else
dojo.byId("check_available_results").innerHTML = "Not Available"
}
function checkLinkInvoke() {
username_input = dojo.byId('id_username')
dojo.io.bind({
url: "/account/isavailable/",
method: "post",
/* optional content. If the content is in the
* form of a hashref they are converted to
* post paramters */
content: {
username: username_input.value,
xhr: 1
},
load: sendFormCallback
});
}
function init() {
var check_link = dojo.byId('checklink');
check_link.style.display = "block"; // enables the check link
dojo.event.connect(check_link, 'onclick', 'checkLinkInvoke');
}
dojo.addOnLoad(init);
</script>
and the associated view:
def available(request):
"""Return the availability status of a desired username.
This is used in conjunction with dojo, on the account registration form.
"""
if not request.POST:
return HttpResponseRedirect(ACCOUNT_REGISTER_URL)
if request.POST.has_key('xhr'):
username = request.POST.get('username', None)
response_dict={}
response_dict['username'] = username
response_dict['available'] = False
if User.objects.filter(username=username).count() == 0:
response_dict['available']=True
return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
else:
return HttpResponseRedirect(ACCOUNT_REGISTER_URL)
|
More like this
- Django Collapsed Stacked Inlines by applecat 1 year, 9 months ago
- Django Collapsed Stacked Inlines by mkarajohn 3 years, 11 months ago
- Dynamically adding forms to a formset. OOP version. by halfnibble 9 years, 6 months ago
- Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 11 years, 7 months ago
- Django admin inline ordering - javascript only implementation by ojhilt 12 years ago
Comments
Please login first before commenting.