Changes between Initial Version and Version 1 of AjaxDojoLogin


Ignore:
Timestamp:
Jun 12, 2006, 12:10:47 AM (18 years ago)
Author:
coulix
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AjaxDojoLogin

    v1 v1  
     1== Adding an ajax login form to your project ==
     2
     3Assuming you have a project running and using the normal authentification scheme providen by django, it would be nice not to reaload the page to show errors about login and password when the login is wrong.
     4
     5We need to add a specific method to your project view.py lets call it '''loginajax'''.
     6Simple we do a copy / past from '''/django/contrib/auth/view.py''' method login.
     7we just change the return answers.
     8
     9{{{
     10#!python
     11def loginajax(request):
     12        manipulator = AuthenticationForm(request)
     13        redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
     14        if request.POST:
     15                errors = manipulator.get_validation_errors(request.POST)
     16                if not errors:
     17                        if not redirect_to or '://' in redirect_to or ' ' in redirect_to:
     18                                redirect_to = '/accounts/profile/'
     19                        request.session[SESSION_KEY] = manipulator.get_user_id()
     20                        request.session.delete_test_cookie()
     21                        return HttpResponse(redirect_to)
     22                else:
     23                        return HttpResponse("false")
     24}}}
     25
     26Now let change our '''ur.py'''
     27You shoudl still keep the normal one r^login/$  and add the new one.
     28{{{
     29#!python
     30[..]
     31        (r'^login/$',  'django.contrib.auth.views.login'),
     32        (r'^login/ajax/$',  'cefinban.views.loginajax'),
     33[...]
     34}}}
     35
     36
     37We then need to add the dojo magic to our login page.
     38id = loginForm refer to our <form id="loginform" ..>
     39If you are confuse see [wiki:AjaxDojoFormSub], similar code is more detailed there.
     40{{{
     41    var sendLoginButton;       
     42       
     43    function sendLogin(){
     44      dojo.io.bind({
     45                    url: '/login/ajax/',
     46                    handler:sendLoginCallback,
     47                    formNode: dojo.byId('loginform'),
     48                    method: 'POST' 
     49                });
     50      // let hide the form
     51      dojo.byId("loginform").style.display = "none";
     52      dojo.byId("messagestatus").innerHTML = "<p>Loading ...</p>";
     53    }
     54
     55    function sendLoginCallback(type, data, evt) {
     56    if (type == 'error')
     57        alert('Error when retrieving data from the server!');
     58    else
     59        if (data === "false") {
     60                 dojo.byId("loginform").style.display = "";
     61                 dojo.byId("messagestatus").innerHTML = "<span id='error'> Your password or username is invalid</span>";
     62                 dojo.event.disconnect(sendLoginButton, 'onclick', 'sendLogin');
     63                 var anim = dojo.lfx.html.highlight("messagestatus", [255, 0, 0], 200).play(100);
     64                 dojo.event.connect(anim, "onEnd", function() {  dojo.event.connect(sendLoginButton, 'onclick', 'sendLogin'); });
     65                 
     66        }
     67        else {
     68                 window.location=data;
     69        }
     70    }
     71 
     72    function init(){
     73        sendLoginButton = dojo.byId('loginButton');
     74        // disable form posting when js used
     75        var loginForm = dojo.byId('loginform');
     76        loginForm.onsubmit = function() { return false; }
     77        dojo.event.connect(sendLoginButton, 'onclick', 'sendLogin');
     78    }
     79 
     80    dojo.addOnLoad(init);
     81}}}
     82
     83
     84Note you can go further by not redirecting to a login_ok page but updating the appropriate field, like the header part of your page
     85telling you if you are a registred user or not (if you have such a thing).
     86
Back to Top