登录页面的django类基础视图

时间:2018-11-03 07:34:46

标签: django-forms django-views

该组中的每个人都好,请尝试使用DJANGO 2.1.3版本创建django个人资料,这是我表格的视图 表格检视 类LoginView(FormView):     template_name ='login.html'     form_class = UserLoginForm     def get_success_url(self):         messages.success(self.request,             “感谢注册”                         )         返回反向('register')     def cleaned_data(自己,请求):         形式= UserLoginForm(request.POST)

    context = {
        'form': form
    }
    if form.is_valid:
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username = username, password = password)
        if not None:
            login(request, user)

这是表格

UserLoginForm(forms.Form)类:

username = forms.CharField(label='Username/Email Address')
password1 = forms.CharField(label='Password', widget=forms.PasswordInput())

def clean_data(self):

    username = self.cleaned_data.get('username')
    password = self.cleaned_data.get('password')

    username = User.objects.filter(
    Q(username = query)|
    Q(email = query)
    )
    if username and password:
        authenticate(username = username, password = password)
        if not username.exists() and username != 1:
            raise ValidationError('invalid user or password')
        username = username.first()
        if not username.check_password(password):
            raise ValidationError("Invalid Username or Password")
        if not username.is_active:
            raise ValidationError('Please Activate Your Account')

    return super(UserLoginForm).clean_data(*args, **kwargs)

0 个答案:

没有答案