[英]“user = authenticate(request, username=username, password=password)” user is none
def login_page(request):
form = LoginForm(request.POST or None)
context = {
"form": form
}
print("User logged in")
#print(request.user.is_authenticated())
if form.is_valid():
print(form.cleaned_data)
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(request, username=username, password=password)
print(user)
print(request.user.is_authenticated())
if user is not None:
print(request.user.is_authenticated())
login(request, user)
# Redirect to a success page.
context['form'] = LoginForm()
return redirect("/")
else:
# Return an 'invalid login' error message.
print("Error")
return render(request, "auth/login.html", context)
Hello, I have started playing around in Django, but in a tutorial, when a tutor clicks submit, it authenticates the user ... I found almost the same problem on stack overflow already, but problem is, that a guy had a string instead of variables ( username = 'username' ) but problem is that when I click submit I get an error : 您好,我已经开始在Django中玩了,但在教程中,当导师点击提交时,它会对用户进行身份验证...我发现堆栈溢出问题几乎一样,但问题是,一个人有一个字符串而不是变量(用户名='用户名'),但问题是,当我点击提交时,我收到一个错误:
User logged in
{'username': 'test123', 'password': 'test'}
None
False
Error
User logged in is just a string in print()
None <- print(user)
False <- print(request.user.is_authenticated())
Error <- else: print("Error")
I am struggling for an hours with this problem ( we have the same version of Django ) Django==1.11.4 我在这个问题上苦苦挣扎了几个小时(我们有相同版本的Django)Django == 1.11.4
So I am not totally sure what exactly is causing your problems here. 所以我不完全确定究竟是什么原因造成你的问题。
I know this probably isn't what they do in the tutorial, but my suggestion to you would be to use the built in Django authentication views. 我知道这可能不是他们在教程中所做的,但我对你的建议是使用内置的Django身份验证视图。 That way you don't have to repeat code that is already done for you.
这样您就不必重复已经为您完成的代码。
The views are very simple to use. 视图使用起来非常简单。 All you need to do is set the proper route in your URL and then create a template under the directory 'registration/login.html'.
您需要做的就是在URL中设置正确的路径,然后在“registration / login.html”目录下创建一个模板。
First set proper settings in your settings.py file(I'm including the login and logout steps because they go hand-in-hand): 首先在settings.py文件中设置正确的设置(我包括登录和注销步骤,因为它们是相辅相成的):
LOGIN_REDIRECT_URL = '/page/you/redirect/to/'
LOGOUT_REDIRECT_URL = '/your/login/page/'
Then set URLs: 然后设置网址:
urls.py urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.conf import settings
urlpatterns = [
url(r'^login/$', auth_views.login, {'redirect_authenticated_user': True},name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': settings.LOGOUT_REDIRECT_URL}, name='logout'),
]
Then finally in your templates folder that is within the same app as the urls.py file where you put the login and logout routes, create a folder named "registration" and create an html file called "login.html". 然后最后在您的模板文件夹中,该文件夹与您放置登录和注销路径的urls.py文件位于同一个应用程序中,创建一个名为“registration”的文件夹并创建一个名为“login.html”的html文件。
Finally, your "login.html" file can simply be this: 最后,您的“login.html”文件可以是这样的:
{% block title %}Login{% endblock %}
{% block content %}
<body>
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
{% endblock %}
When you want to logout, just put a button wherever you want and link it to "/logout". 当您想要注销时,只需在任意位置放置一个按钮并将其链接到“/ logout”即可。 And after that, authentication is done!
之后,身份验证就完成了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.