|
8鱼币
本帖最后由 tengke 于 2023-6-16 11:57 编辑
class Logina(forms.Form):
username = forms.CharField(label="用户名",
widget=forms.TextInput(attrs={"class":"form-control","maxlength":"12","placeholder":"请输入用户名","autocomplete":"off"}))
password = forms.CharField(label="密码",
widget=forms.PasswordInput(attrs={"class":"form-control","maxlength":"12","placeholder":"请输入密码","autocomplete":"off"}))
yzm = forms.CharField(label="验证码",
widget=forms.PasswordInput(attrs={"class": "form-control", "maxlength": "12", "placeholder": "验证码","autocomplete": "off"}))
def clean(self):
cleaned_data = super().clean()
pwd = md5(cleaned_data.get("password"))
print(pwd,"clean的密码")
cleaned_data["password"] = pwd
return cleaned_data
____
def yzm(request):
img,code_staing = check_code()
print(code_staing)
#写入到自己的session中
request.session['image_code']=code_staing
#图片60秒超时
request.session.set_expiry(60)
stream = BytesIO()
img.save(stream, 'png')
# stream.getvalue()
return HttpResponse(stream.getvalue())
_________
def login(request):
if request.method == "GET":
form = Logina()
return render(request,"login.html",{"form":form})
form = Logina(data=request.POST)
print(form.is_valid(),"formis")
print(form.errors.as_json())
# print(form,"from 字段属性")
if form.is_valid():
# print(form.cleaned_data)
# pd = models.admin.objects.filter(username=form.cleaned_data["username"],password=form.cleaned_data["password"])
pd = models.admin.objects.filter(**form.cleaned_data).first()
print(form.cleaned_data,"cleaned_data")
print(form.cleaned_data['yzm'])
print(form.errors.as_json())这个打印出来的是
{"yzm": [{"message": "\u8fd9\u4e2a\u5b57\u6bb5\u662f\u5fc5\u586b\u9879\u3002", "code": "required"}]}
麻烦大佬看看这个
你的代码看起来已经能够做到验证码的生成和验证了。然而,你可能在调试过程中遇到了一些问题。在你的代码中,我注意到了一些可能存在问题的地方:
1. 你在 Logina 表单中定义了一个 yzm 字段,它是一个验证码输入框。但是你将验证码输入框的 widget 设为了 forms.PasswordInput ,这会导致用户输入的验证码字符被显示为星号或圆点,这在实际应用中可能不是你希望的效果。如果你想让验证码字符在输入时显示出来,你应该使用 forms.TextInput 。
2. 验证码大小写的问题。在许多情况下,为了提高用户体验,我们通常会忽略验证码的大小写。然而在你的代码中,你直接将用户输入的验证码和生成的验证码进行了比较,这可能会导致大小写不匹配的问题。为了解决这个问题,你可以在比较之前将两个验证码都转换为小写或大写。
3. 你的验证逻辑中,如果验证码错误,会返回一个带有错误信息的表单。然而如果用户密码错误,你的代码也会返回一个带有错误信息的表单,但是这个表单的错误信息是 "用户名密码错误",这可能会让用户感到困惑。你应该明确指出是用户名错误还是密码错误。
修改后的代码片段如下:
class Logina(forms.Form):
username = forms.CharField(label="用户名",
widget=forms.TextInput(attrs={"class":"form-control","maxlength":"12","placeholder":"请输入用户名","autocomplete":"off"}))
password = forms.CharField(label="密码",
widget=forms.PasswordInput(attrs={"class":"form-control","maxlength":"12","placeholder":"请输入密码","autocomplete":"off"}))
yzm = forms.CharField(label="验证码",
widget=forms.TextInput(attrs={"class": "form-control", "maxlength": "12", "placeholder": "验证码","autocomplete": "off"}))
def login(request):
if request.method == "GET":
form = Logina()
return render(request,"login.html",{"form":form})
form = Logina(data=request.POST)
if form.is_valid():
# print(form.cleaned_data)
# pd = models.admin.objects.filter(username=form.cleaned_data["username"],password=form.cleaned_data["password"])
pd = models.admin.objects.filter(**form.cleaned_data).first()
print(form.cleaned_data,"cleaned_data")
userinputcode = form.cleaned_data.pop('yzm').lower()
# 剔除 yzm信息
yzm = request.session.get('image_code', "").lower()
if yzm != userinputcode:
form.add_error("yzm", "验证码错误")
return render(request, "login.html", {"form": form})
# ... 其他逻辑
注意,以上只是建议,具体应该如何修改,还需要根据你的实际情况来决定。
|
|