如何在没有 Flask-WTF 的情况下使用 recaptcha
How to use recaptcha without Flask-WTF
我制作一个文件共享网站已经有一段时间了,我想在人们注册到该网站时实施验证码。问题是我不能使用 Flask-WTF,因为我将不得不更改很多代码(我一直在没有它的情况下编程)。
我发现这个 Flask recaptcha 不包括 Flask-WTF 的使用,但我似乎无法让它工作(它不显示 recaptcha 本身):
https://github.com/mardix/flask-recaptcha
我已经一步步跟着做了,还是不行。我唯一没有做的是配置。
编辑:
验证码不工作。每次我输入正确的注册信息并标记验证码时,它都会说 username/password 不正确。我不标记它也是一样的
这是验证码(其他人之前用过):
recaptcha = ReCaptcha(app=app)
if recaptcha.verify() is False:
flash('Captcha is incorrect')
return redirect(url_for('register'))
<div id="captcha"">
{{ recaptcha }} - HTML PART
</div>
编辑:在得到 Nurzhan 的帮助后,我更改了代码,无论如何验证码总是 returns 错误。
您没有尝试配置,但您需要指明密钥才能使您的 recaptcha 正常工作。这 2 个选项在配置中不是可选的:
RECAPTCHA_SITE_KEY : Public key
RECAPTCHA_SECRET_KEY: Private key
为它们设置适当的值,看看它是否有效。
编辑:
现在可以了。这是 app.py:
import requests
import json
from flask import Flask, render_template, request
from flask_recaptcha import ReCaptcha
app = Flask(__name__)
app.config.update({'RECAPTCHA_ENABLED': True,
'RECAPTCHA_SITE_KEY':
'site_key',
'RECAPTCHA_SECRET_KEY':
'secret_key'})
recaptcha = ReCaptcha(app=app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['GET', 'POST'])
def submit():
print('SUBMIT CALLED')
username = ''
password = ''
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
print(request.form)
if username == 'username' and password == 'password':
print('CREDENTIALS ARE OK')
r = requests.post('https://www.google.com/recaptcha/api/siteverify',
data = {'secret' :
'secret_key',
'response' :
request.form['g-recaptcha-response']})
google_response = json.loads(r.text)
print('JSON: ', google_response)
if google_response['success']:
print('SUCCESS')
return render_template('profile.html')
else:
# FAILED
print('FAILED')
return render_template('index.html')
# if recaptcha.verify():
# # SUCCESS
app.run(debug=True)
这是 index.html 页面:
<!DOCTYPE html>
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Flask Recaptcha</h1>
<p>Flask Recaptcha Test</p>
<form method="post" action="/submit">
Username:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="password" name="password">
{{ recaptcha }}
<input type="submit" value="Submit">
<div class="g-recaptcha" data-sitekey="site_key"></div>
</form>
</body>
</html>
如果您通过验证,这是profile.html页面:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Profile page</h1>
<p>Registration is ok</p>
</body>
</html>
我无法让 recaptcha.verify()
工作。在 Google Recaptcha 的官方文档中声明您需要在客户提交您的表单和您的 secret_key
和 g-recaptcha-response
当用户在 recaptcha 中打勾时你会收到。
请注意,这只是示例代码。您需要将自己的 site_key 和 secret_key 添加到 app.py
和 index.html
中,并添加对用户凭据的正确检查以进行注册,例如重复输入密码等。
我制作一个文件共享网站已经有一段时间了,我想在人们注册到该网站时实施验证码。问题是我不能使用 Flask-WTF,因为我将不得不更改很多代码(我一直在没有它的情况下编程)。
我发现这个 Flask recaptcha 不包括 Flask-WTF 的使用,但我似乎无法让它工作(它不显示 recaptcha 本身):
https://github.com/mardix/flask-recaptcha
我已经一步步跟着做了,还是不行。我唯一没有做的是配置。
编辑: 验证码不工作。每次我输入正确的注册信息并标记验证码时,它都会说 username/password 不正确。我不标记它也是一样的
这是验证码(其他人之前用过):
recaptcha = ReCaptcha(app=app)
if recaptcha.verify() is False:
flash('Captcha is incorrect')
return redirect(url_for('register'))
<div id="captcha"">
{{ recaptcha }} - HTML PART
</div>
编辑:在得到 Nurzhan 的帮助后,我更改了代码,无论如何验证码总是 returns 错误。
您没有尝试配置,但您需要指明密钥才能使您的 recaptcha 正常工作。这 2 个选项在配置中不是可选的:
RECAPTCHA_SITE_KEY : Public key
RECAPTCHA_SECRET_KEY: Private key
为它们设置适当的值,看看它是否有效。
编辑:
现在可以了。这是 app.py:
import requests
import json
from flask import Flask, render_template, request
from flask_recaptcha import ReCaptcha
app = Flask(__name__)
app.config.update({'RECAPTCHA_ENABLED': True,
'RECAPTCHA_SITE_KEY':
'site_key',
'RECAPTCHA_SECRET_KEY':
'secret_key'})
recaptcha = ReCaptcha(app=app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['GET', 'POST'])
def submit():
print('SUBMIT CALLED')
username = ''
password = ''
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
print(request.form)
if username == 'username' and password == 'password':
print('CREDENTIALS ARE OK')
r = requests.post('https://www.google.com/recaptcha/api/siteverify',
data = {'secret' :
'secret_key',
'response' :
request.form['g-recaptcha-response']})
google_response = json.loads(r.text)
print('JSON: ', google_response)
if google_response['success']:
print('SUCCESS')
return render_template('profile.html')
else:
# FAILED
print('FAILED')
return render_template('index.html')
# if recaptcha.verify():
# # SUCCESS
app.run(debug=True)
这是 index.html 页面:
<!DOCTYPE html>
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Flask Recaptcha</h1>
<p>Flask Recaptcha Test</p>
<form method="post" action="/submit">
Username:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="password" name="password">
{{ recaptcha }}
<input type="submit" value="Submit">
<div class="g-recaptcha" data-sitekey="site_key"></div>
</form>
</body>
</html>
如果您通过验证,这是profile.html页面:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Profile page</h1>
<p>Registration is ok</p>
</body>
</html>
我无法让 recaptcha.verify()
工作。在 Google Recaptcha 的官方文档中声明您需要在客户提交您的表单和您的 secret_key
和 g-recaptcha-response
当用户在 recaptcha 中打勾时你会收到。
请注意,这只是示例代码。您需要将自己的 site_key 和 secret_key 添加到 app.py
和 index.html
中,并添加对用户凭据的正确检查以进行注册,例如重复输入密码等。