[Django]-Django: save referer when user is signing up

6👍

First, save the referrer to the session. You should probably do this in some kind of middleware:

import urlparse

class SaveReferrerMiddleware(object):
    def process_request(self, request):
        referer = request.META.get('HTTP_REFERER', None)
        if referer is not None:
            domain = urlparse.urlparse(referer).netloc
            if domain not in ['www.yoursite.com', 'yoursite.com']:
                # External referer
                request.session['initial_referer'] = referer

(Obviously, change yoursite.com etc. as appropriate)

Then, in your signup view code, save the referer from the session to your user object.

This will, of course, depend on having somewhere to store that info. If you have a custom user object, you can create a field for it. Before Django 1.5, you can use AUTH_PROFILE_MODULE to extend the user class. See https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

Leave a comment