我创建了测验应用程序,其中仅用用户名登录的用户登录,并进行了测验,但是django身份验证不起作用 views.py
class UserFormView(View):
form_class = UserForm
template_name = 'app/registration_form.html'
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned data
username = form.cleaned_data['username']
user.save()
# autheticate
user = authenticate(username=username)
if user is not None:
if user.is_active:
login(request, user)
return redirect('app:quiz_list')
else:
return HttpResponse('active nahi hai')
else:
return HttpResponse('pehchan me nahi hai')
return render(request, self.template_name, {'form': form})
答案 0 :(得分:0)
如果您使用的是默认身份验证后端,则需要用户名和密码
您可以按照相关的documentation
自己创建自定义身份验证后端考虑到您并未真正进行身份验证,您可以从数据库中过滤用户并随后使用登录功能
#TODO add exception if there is no user with username
user = User.objects.filter(username=username).get()
login(request, user)