我想将Recaptcha添加到我的登录表单中。我正在关注this存储库,但遇到了一些问题:我将此添加到了views.py
:
from django import forms
from captcha.fields import ReCaptchaField
class FormWithCaptcha(forms.Form):
captcha = ReCaptchaField()
但是我真的不知道从这里去哪里。我想我需要在login.html
页面上添加一些内容,但是我不知道是什么。谁能给我些帮助吗?请注意,我已经将公用密钥和专用密钥添加到了settings.py
文件中。
这是整个views.py
:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Tutorial
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, logout, authenticate
from django.contrib import messages
from .forms import NewUserForm
from django import forms
from captcha.fields import ReCaptchaField
class FormWithCaptcha(forms.Form):
captcha = ReCaptchaField()
def homepage(request):
return render(request=request,
template_name="main/home.html",
context={"tutorials": Tutorial.objects.all})
def register(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, f"New Account Created: {username}")
login(request, user)
messages.info(request, f"You are now logged in as {username}")
return redirect("main:homepage")
else:
for msg in form.error_messages:
messages.error(request, f"{msg}: {form.error_messages[msg]}")
form = NewUserForm
return render(request,
"main/register.html",
context={"form":form})
def logout_request(request):
logout(request)
messages.info(request, "Logged out successfully!")
return redirect("main:homepage")
def login_request(request):
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
messages.info(request, f"You are now logged in as {username}")
return redirect("main:homepage")
else:
messages.error(request, "Invalid username or password")
else:
messages.error(request, "Invalid username or password")
form = AuthenticationForm()
return render(request,
"main/login.html",
{"form":form})
答案 0 :(得分:1)
您应该发布views.py
,以便为您提供更好的帮助,但是使用Django的常识,您应该使用FormWithCaptcha作为上下文变量(例如,名为recaptcha)呈现login.html
文件,然后在您的登录文件在任何需要的地方都可以像{{recaptcha}}一样命名。但同样,如果您的问题尚未解决,请发布您的意见文件。
def login_request(request):
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
messages.info(request, f"You are now logged in as {username}")
return redirect("main:homepage")
else:
messages.error(request, "Invalid username or password")
else:
messages.error(request, "Invalid username or password")
form = AuthenticationForm()
recaptcha = FormWithCaptcha()
return render(request,
"main/login.html",
{"form":form, "recaptcha": recaptcha})
答案 1 :(得分:1)
您可以通过从django.contrib.auth继承LoginForm的子类来实现目标,而无需外部库
有关代码示例和更详细的说明,请参见this答案。您还可以使用上下文处理器将recaptcha公钥添加到login.html模板中。