Define a special set of URLs (in your urls.py) for referral links. Pass them through a Python function that registers the referral and then send them to the normal view with the referral number as a parameter.
Is this what you had in mind, or what else would you like to know?