💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
>[success] # 直接在Form 做session 判断 >[danger] ##### 直接判断的代码 ~~~ def clean(self): value_dict = self.cleaned_data password = value_dict.get("password") username = value_dict.get("username") check_box = value_dict.get("check_box") userinfo_obj = models.UserInfo.objects.filter(username=username,password=password).values("username", "nickname", "email").first() if not userinfo_obj: raise ValidationError("用户名或密码错误") else: self.request.session['is_login'] = True self.request.session['userinfo'] = userinfo_obj if check_box: self.request.session.set_expiry(60 * 60 * 24 * 7) return value_dict ~~~ >[danger] ##### 勾选登陆时间input便签 ~~~ check_box = fields.CharField( required=False, widget=widgets.CheckboxInput({'value':0}) ) ~~~ >[success] # 所有代码 ~~~ from django.forms import Form from django.forms import fields from django.forms import widgets from cm import models from django.core.exceptions import ValidationError class LoginForm(Form): def __init__(self,request,*args,**kwargs): self.request = request super(LoginForm,self).__init__(*args,**kwargs) username = fields.CharField( max_length="32", widget=widgets.TextInput(attrs={}), error_messages={"required":"不能为空", "max_length":"最大长度32"} ) password = fields.CharField( widget=widgets.TextInput(attrs={}), error_messages={"required": "不能为空"} ) code = fields.CharField( max_length=4, min_length=4, widget=widgets.TextInput(attrs={}), error_messages={"required":"请填写验证码","max_length":"请填写正确验证码","min_length":"验证码错误"} ) check_box = fields.CharField( required=False, widget=widgets.CheckboxInput({'value':0}) ) def clean_code(self): v = self.cleaned_data['code'] if self.request.session.get("CheckCode").upper() != v.upper(): raise ValidationError("验证码错误") return v def clean(self): value_dict = self.cleaned_data password = value_dict.get("password") username = value_dict.get("username") check_box = value_dict.get("check_box") userinfo_obj = models.UserInfo.objects.filter(username=username,password=password).values("username", "nickname", "email").first() if not userinfo_obj: raise ValidationError("用户名或密码错误") else: self.request.session['is_login'] = True self.request.session['userinfo'] = userinfo_obj if check_box: self.request.session.set_expiry(60 * 60 * 24 * 7) return value_dict ~~~