在用户登录期间,使用正确的用户名和密码进行身份验证返回 None [英] Authenticate returns None with correct username and password during Userlogin
问题描述
我的用户登录在身份验证过程中存在一些问题.我正在使用 Django 1.9 和 Python 3.6
<块引用>user = authentication(username=username, password=password)
返回用户为无
这是我的Accounts/views.py寻找登录信息的方式
def 注册(请求):注册 = 错误如果 request.method == 'POST':reg_form = RegistrationForm(data=request.POST)profile_form = UserProfileForm(data=request.POST)如果 reg_form.is_valid() 和 profile_form.is_valid():用户 = reg_form.save()# print('before set password = ', user.password)user.set_password(user.password)# print('设置密码后 = ', user.password)用户.save()打印(用户.密码)profile = profile_form.save(commit=False)profile.user = 用户profile.email = 用户.emailprofile.first_name = user.first_nameprofile.last_name = user.last_name如果 request.FILES 中的profile_pic":profile.profile_pic = request.FILES['profile_pic']打印('上传图片.....')profile.save()args = {'reg_form': reg_form, 'profile_form': profile_form, 'registered': True}head_list.update(args)返回渲染(请求,'registration.html',head_list)别的:打印(reg_form.errors,profile_form.errors)args = {'reg_form': reg_form.errors, 'profile_form': profile_form.errors, 'registered': False}head_list.update(args)返回渲染(请求,'registration.html',head_list,args)别的:reg_form = 注册表格()profile_form = UserProfileForm()args = {'reg_form': reg_form, 'profile_form': profile_form, '注册': False}head_list.update(args)打印(head_list)返回渲染(请求,'registration.html',head_list)定义登录视图(请求):参数 = {}params.update(csrf(请求))如果 request.method == 'POST':form = LoginForm(request.POST)如果 form.is_valid():用户名 = form.cleaned_data.get('用户名')密码 = form.cleaned_data.get('密码')# 首先获取提供的用户名和密码# username = request.POST.get('用户名', '')# 密码 = request.POST.get('密码', '')# Django 内置的认证功能:打印(用户名,密码)用户 = 验证(用户名 = 用户名,密码 = 密码)打印('验证后',用户)# 如果我们有一个用户如果用户:# 检查账户是否活跃如果 user.is_active:# 登录用户.登录(请求,用户名)# 将用户发送回某个页面.# 在这种情况下,他们的主页.# return HttpResponseRedirect(reverse('/user_login/'))return render_to_response('user_login.html', RequestContext(request, {}))别的:# 如果账户不活跃:return HttpResponse("您的帐户未激活.")别的:print("有人尝试登录但失败了.")print("他们使用了用户名:{} 和密码:{}".format(username, password))return HttpResponse("提供的登录信息无效.")别的:表单 = 登录表单()args = {'形式':形式}head_list.update(args)# 没有提供用户名或密码.返回渲染(请求,'login.html',head_list)
login.html 页面如下所示
{% 块内容 %}<section class="容器"><h1>LiquorApp 登录控制台</h1><div class="登录"><h1>登录到WebApp</h1><form method="post" action="/user_login/">{% csrf_token %}{{ form.as_p }}{% comment %}用户名:<input type="text" name="username" value="" size="50"/><br/>{% endcomment %}{% comment %}<p><input type="text" name="username" value="" id="username" placeholder="username"></p><p><input id ="password" type="password" name="password" value="" placeholder="password"></p><p class="remember_me">{% endcomment %}<标签><input type="checkbox" name="remember_me" id="remember_me">在这台电脑上记住我标签></p><p class="submit"><input type="submit" name="commit" value="登录"></p></表单>
</节>{% 结束块 %}
请指出我的身份验证模块没有返回任何错误.
我还在 settings.py 文件中添加了以下内容
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
移除
user.set_password(user.password)
来自 Accounts.views.register
My User login has some issue with the authentication process. I am using Django 1.9 and Python 3.6
user = authenticate(username=username, password=password)
Returns user as none
This is how my Accounts/views.py looks for login
def register(request):
registered = False
if request.method == 'POST':
reg_form = RegistrationForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
if reg_form.is_valid() and profile_form.is_valid():
user = reg_form.save()
# print('before set password = ', user.password)
user.set_password(user.password)
# print('after set password = ', user.password)
user.save()
print(user.password)
profile = profile_form.save(commit=False)
profile.user = user
profile.email = user.email
profile.first_name = user.first_name
profile.last_name = user.last_name
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
print('uploading pic .....')
profile.save()
args = {'reg_form': reg_form, 'profile_form': profile_form, 'registered': True}
head_list.update(args)
return render(request, 'registration.html', head_list)
else:
print(reg_form.errors, profile_form.errors)
args = {'reg_form': reg_form.errors, 'profile_form': profile_form.errors, 'registered': False}
head_list.update(args)
return render(request, 'registration.html', head_list, args)
else:
reg_form = RegistrationForm()
profile_form = UserProfileForm()
args = {'reg_form': reg_form, 'profile_form': profile_form, 'registered': False}
head_list.update(args)
print(head_list)
return render(request, 'registration.html', head_list)
def login_view(request):
params = {}
params.update(csrf(request))
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
# First get the username and password supplied
# username = request.POST.get('username', '')
# password = request.POST.get('password', '')
# Django's built-in authentication function:
print(username, password)
user = authenticate(username=username, password=password)
print('after aunthenticate', user)
# If we have a user
if user:
# Check it the account is active
if user.is_active:
# Log the user in.
login(request, username)
# Send the user back to some page.
# In this case their homepage.
# return HttpResponseRedirect(reverse('/user_login/'))
return render_to_response('user_login.html', RequestContext(request, {}))
else:
# If account is not active:
return HttpResponse("Your account is not active.")
else:
print("Someone tried to login and failed.")
print("They used username: {} and password: {}".format(username, password))
return HttpResponse("Invalid login details supplied.")
else:
form = LoginForm()
args = {'form': form}
head_list.update(args)
# Nothing has been provided for username or password.
return render(request, 'login.html', head_list)
The login.html page is shown below
{% block content %}
<section class="container">
<h1>LiquorApp Login Console</h1>
<div class="login">
<h1>Login to WebApp</h1>
<form method="post" action="/user_login/">
{% csrf_token %}
{{ form.as_p }}
{% comment %}Username: <input type="text" name="username" value="" size="50" />
<br />{% endcomment %}
{% comment %}<p><input type="text" name="username" value="" id="username" placeholder="username"></p>
<p><input id ="password" type="password" name="password" value="" placeholder="password"></p>
<p class="remember_me">{% endcomment %}
<label>
<input type="checkbox" name="remember_me" id="remember_me">
Remember me on this computer
</label>
</p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
</div>
</section>
{% endblock %}
Please suggest where am I doing it wrong that my authenticate module is returning none.
I have also added the following in the settings.py file
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
Remove
user.set_password(user.password)
from Accounts.views.register
这篇关于在用户登录期间,使用正确的用户名和密码进行身份验证返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!