Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to use a captcha, you can use the <a href="https://flask-wtf.readthedocs.org/en/latest/form.html#recaptcha" rel="nofollow">feature that's already built into Flask-WTF</a> and save yourself reinventing the wheel. </p> <p>If you do want to reinvent the wheel, then the main problem you're having is that you're recreating the captcha when the user submits the form, you have no way to remember and refer to the old value.</p> <p>So this is how it's working at the moment:</p> <ul> <li>User goes to sign in, you generate a captcha, then because they haven't submitted a form, it shows the sign in form including the captcha picture.</li> <li>User fills in the form and hits the submit button- this loads the signup view again,<strong>creates a new random captcha</strong>, then goes down the form submitted logic path, so when you compare the user captcha data to the current captcha data, it doesn't match.</li> </ul> <p>So you're missing persistence, the captcha you generate the first time round doesn't get held anywhere, so when the user submits you've got no way to refer back to it. So you need to store that captcha word somewhere. You could simply just store that captcha word in the user's session and use that to validate against when you need to, or perhaps sign it with <a href="https://flask-wtf.readthedocs.org/en/latest/form.html#recaptcha" rel="nofollow">itsdangerous</a> and store it in the form itself as a hidden field?</p> <h3>Code Example:</h3> <p>This just takes your code and adjusts it a little to store the value in the session-- not tested, and can definitely been improved, but should work:</p> <pre><code>@app.route('/signup', methods = ['GET', 'POST']) def signup(): if g.user is not None and g.user.is_authenticated(): return redirect(url_for('index')) if request.method == 'post': captcha_word = session["captcha"] else: words = open('app/corncob_caps.txt').readlines() captcha_word = words[random.randint(1,len(words))] session["captcha"] = captcha_word captcha_filename = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10)) + '.jpg' captcha.gen_captcha(captcha_word.strip(), 'app/os.ttf', 25, 'app/static/' + captcha_filename + '') form = SignUpForm(captcha_word) if form.validate_on_submit() == False: return render_template('signup.html', form = form, filename = captcha_filename) else: user = User(form.email.data, form.password.data) db.session.add(user) db.session.commit() flash('You have successfully signed up.') flash('You may login now.') return redirect(url_for('login')) return render_template('signup.html', form = form, filename = captcha_filename) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload