django 表单没有错误
django form no errors
我正在尝试创建一个自定义登录表单,当我验证该表单时没有错误并且它是无效的
有没有更好的方法来实现我想要的?至少我只想从表单字段中获取值。
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control',
'placeholder': '',
}
))
def login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
print(True)
else:
print(False)
print(username)
print(password)
# authUrl = ""
# authData = {'usname': request.POST['username'],
# 'psword': request.POST['password'],
# }
# s = requests.Session()
# rAuth = s.post(authUrl, data=authData).json()
# if rAuth["status"] == 200:
# pass
else:
form = LoginForm()
return render(request, 'users/login.html', {'form': form})
我一般都是这样使用自己的表格
表格
class LoginForm(forms.Form):
email = forms.EmailField(label="Email")
password = forms.CharField(label="Password", widget=forms.PasswordInput())
def clean(self, *args, **kwargs):
email = self.cleaned_data.get("email")
password = self.cleaned_data.get("password")
user = authenticate(username=email, password=password)
if not user:
raise forms.ValidationError("Failed to authenticate this user")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password")
if not user.is_active:
raise forms.ValidationError("This user has been deactivated")
return super(LoginForm, self).clean(*args, **kwargs)
查看
class LoginView(View):
def get(self, request):
context = {
'form': LoginForm()
}
return render(request, "www/login.html", context)
def post(self, request):
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(username=request.POST.get("email"), password=request.POST.get("password"))
login(request, user)
return redirect('index')
context = {'form': form}
return render(request, "www/login.html", context)
希望这对你有所帮助
我正在尝试创建一个自定义登录表单,当我验证该表单时没有错误并且它是无效的
有没有更好的方法来实现我想要的?至少我只想从表单字段中获取值。
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control',
'placeholder': '',
}
))
def login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
print(True)
else:
print(False)
print(username)
print(password)
# authUrl = ""
# authData = {'usname': request.POST['username'],
# 'psword': request.POST['password'],
# }
# s = requests.Session()
# rAuth = s.post(authUrl, data=authData).json()
# if rAuth["status"] == 200:
# pass
else:
form = LoginForm()
return render(request, 'users/login.html', {'form': form})
我一般都是这样使用自己的表格
表格
class LoginForm(forms.Form):
email = forms.EmailField(label="Email")
password = forms.CharField(label="Password", widget=forms.PasswordInput())
def clean(self, *args, **kwargs):
email = self.cleaned_data.get("email")
password = self.cleaned_data.get("password")
user = authenticate(username=email, password=password)
if not user:
raise forms.ValidationError("Failed to authenticate this user")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password")
if not user.is_active:
raise forms.ValidationError("This user has been deactivated")
return super(LoginForm, self).clean(*args, **kwargs)
查看
class LoginView(View):
def get(self, request):
context = {
'form': LoginForm()
}
return render(request, "www/login.html", context)
def post(self, request):
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(username=request.POST.get("email"), password=request.POST.get("password"))
login(request, user)
return redirect('index')
context = {'form': form}
return render(request, "www/login.html", context)
希望这对你有所帮助