Search code examples
djangodjango-registration

tracking user login with django


I am using the builtin login module to perform logins in my website

url(r'^login/$', 'django.contrib.auth.views.login', {'extra_context': {'page_name': 'login'}),

and this is the next action of my login form

<input type="hidden" name="next" value="/redirect/">

During registration I create a small tracking code for the user like this:

host = request.META.get('HTTP_X_FORWARDED_FOR','') or request.META.get('REMOTE_ADDR')
if tid == '0':
    import os, binascii
    tid = binascii.hexlify(os.urandom(6))
ut = UserTracking.objects.create(user=user, username=username, tracking_id=tid, remote_host=host, action='register')
ut.save()
variables = RequestContext(request, {'username': form.cleaned_data['username'], 'email': form.cleaned_data['email'], 'message': message, 'tid': tid})
return render_to_response('registration/register_success.html', variables)

In the register_success.html I use a jquery plugin to save my tracking code into the user's pc like this:

$.jStorage.set('tid', '{{ tid }}');

How can I add this code to my login form and send in with username and password to my views.py?


Solution

  • You might need to customize the login view.

    1. Add your login view:

      url(r'^login/$', 'myapp.account.views.login', {'extra_context': {'page_name': 'login'})

    Then in your myapp/account/views.py, add:

    def login(request, template_name='logintemplate.html'):
    
        if (request.POST):
            username = request.POST.get('username', None)
            password = request.POST.get('password', None)
            tracking_code = request.POST.get('tracking_code', None)
            user = authenticate(username=username, password=password)
    ......
        if user:
            # your custom code here
    ......
    

    and in logintemplate.html add the tracking code into the login form.