Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Question about Session from a Newbie

I have a email sign up page that takes in ?ref= referral codes and I've noticed something weird after I have entered one ?ref= , any further sign ups without a ?ref= will still use the referral code. I assume the ref data is stored for some time and is being used untill it is cleared or new data is entered.

Is this only an issue because I'm creating accounts from the same pc or would someone signing up from another location still have the ref stored in session?


Middleware


from joins.models import Join


class ReferMiddleware():
def process_request(self, request):
ref_id = request.GET.get("ref")
try:
obj = Join.objects.get(ref_id = ref_id)
except:
obj = None
if obj:
request.session['join_id_ref'] = obj.id


View


def home(request):
try:
join_id = request.session['join_id_ref']
obj = Join.objects.get(id=join_id)
except:
obj = None

form = JoinForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=False)
email = form.cleaned_data['email']
new_join_old, created = Join.objects.get_or_create(email=email)
if created:
new_join_old.ref_id = get_ref_id()
# add our friend who referred us to our join model or a related
if not obj == None:
new_join_old.friend = obj
new_join_old.ip_address = get_ip(request)
new_join_old.save()

#print all "friends" that joined as a result of main sharer email
#print Join.objects.filter(friend=obj).count()
#print obj.referral.all().count()

#redirect here
return HttpResponseRedirect("/%s" %(new_join_old.ref_id))

context = {"form": form}
template = "home.html"
return render(request, template, context)





/r/django
https://redd.it/5q4wnt