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)
|
Comments