I'm creating some sort of referral page like the famous one mentioned on Tim Ferris blog but this one in Flask. I have been testing my code with some print statements to see what was happening in the terminal.
Seems like in main() view I'm losing the value stored in unique_code after form.validate_on_submit(). What is happening behind the scenes to get this kind of behavior?
What I'm trying to accomplish is the following:
If the URL is something like [domain]/referral_url/[random unique string] means like this user was referred by another one. So I grab from the URL the random unique string and pass it to the function view, this way when I record the email of this user within the database I can also store who referred this one.
After validation, unique_code when printing in the terminal, in case I print it after validation, it returns to None (default value). Before validation it returns the given unique_code.
Is this a matter of the scope?
For now the code is not finished. Here you have a piece of my views.
@app.route('/', methods=['GET', 'POST'])
@app.route('/referral_url/<unique_code>', methods=['GET', 'POST'])
def main(unique_code=None):
form = SubscriptionForm()
# if I print here unique_code it returns the proper one
if form.validate_on_submit():
# after validation unique_code returns to default, None in this case
# for now if we pass validation we are losing unique_code value, looks like empty despite not being so
if unique_code == True:
return unique_code
else:
if Subscriber.query.filter_by(email=form.email.data).count() == 0:
subscriber = Subscriber(email=form.email.data)
db.session.add(subscriber)
db.session.commit()
else:
subscriber = Subscriber.query.filter_by(email=form.email.data).one()
session['subscriber'] = form.email.data
return redirect(url_for('dashboard', unique_code = subscriber.unique_url))
return render_template('subscribe/main.html', form=form, unique_code=unique_code)