我正在使用Django 1.8并实现了自定义用户模型。用户注册件100%功能;我可以提交表单并验证用户是否已创建。但我正在努力用户登录过程。
登录表单渲染得很好,但是当我输入我已验证的用户名和密码时(通过Django管理员验证),我会看到 HttpResponse('表格无效' )消息。
我被困在这一天一两天。非常感谢任何建议!
帐户/ views.py
from django.views.generic import FormView
from django.contrib.auth import authenticate, login
from django.shortcuts import render
from accounts.forms import CustomUserCreationForm, CustomUserLoginForm
from accounts.models import CustomUser
class CustomUserCreateView(FormView):
form_class = CustomUserCreationForm
template_name = 'registration/registration_form.html'
success_url = '/connections/'
def form_valid(self, form):
form.save()
return super(CustomUserCreateView, self).form_valid(form)
class CustomUserLoginView(FormView):
form_class = CustomUserLoginForm
template_name = 'registration/login.html'
success_url = '/success/'
def get(self, request, *args, **kwargs):
form = self.form_class(initial=self.initial)
return render(request, self.template_name, {'form':form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
user = authenticate(
username=form.cleaned_data['email'],
password=form.cleaned_data['password'],
)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(success_url)
else:
return HttpResponse('User is not active') # TEMP
else:
return HttpResponse('User does not exist') # TEMP
else:
return HttpResponse('Form is invalid') # TEMP
帐户/ forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from .models import CustomUser
class CustomUserLoginForm(AuthenticationForm):
model = CustomUser
# TODO - need to provide error message when no user is found
class CustomUserCreationForm(UserCreationForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('first_name', 'last_name', 'email', 'mobile_number')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError('Passwords do not match!')
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
答案 0 :(得分:0)
该错误意味着您的"帖子" " CustomUserLoginView"中的方法没有返回HttpResponse,因为你很少有"传递"而不是返回正确的响应。因为在少数情况下你什么也不做,所以方法的底部到了,默认情况下python函数/方法返回None。您只在一种情况下返回HttpResponse(当user.is_active时)。您应该看到" if-else"的哪个分支正在通过。你必须在所有情况下(总是)返回一个HttpResponse。
玩得开心!
答案 1 :(得分:0)
This answer最终导致我解决了问题。
在'post'方法中,我需要更改以下行:
form = self.form_class(request.POST)
为:
form = self.form_class(data=request.POST)
最后,我的CustomUserLoginView如下所示:
class CustomUserLoginView(FormView):
form_class = AuthenticationForm
template_name = 'registration/login.html'
success_url = '/connections/'
def get(self, request, *args, **kwargs):
form = self.form_class(initial=self.initial)
return render(request, self.template_name, {'form':form})
def post(self, request, *args, **kwargs):
form = self.form_class(data=request.POST)
if form.is_valid():
user = authenticate(
username=form.cleaned_data['username'],
password=form.cleaned_data['password'],
)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(self.success_url)
else:
return HttpResponse('User is not active') # TEMP
else:
return HttpResponse('User does not exist') # TEMP
else:
return HttpResponse('Form is not valid') # TEMP