简体   繁体   English

使用User authentication()的Django用户登录错误

[英]Django user login error using User authentication()

I have a basic login, sign up html page in my Django project: the login page redirects to the user_login function in views.py as follows: 我有一个基本的登录名,在我的Django项目中注册了html页面: 登录页面重定向到 views.py中的user_login函数,如下所示:

  <form action="{% url 'user_login' %}" method="POST">. //rest of the code

In urls.py the request is getting forwaded correctly: urls.py中 ,请求已正确被放弃:

   .......#beginnning code

   path('user_login', views.user_login, name='user_login'),
   path('portfolio', views.portfolio, name='portfolio'),  
   ......

In views.py this is my user_login code to authenticate the user and redirect the user to a 'portfolio.html' page if the user credentials are correct. 在views.py中,这是我的user_login代码,用于验证用户身份,如果用户凭据正确,则将用户重定向到“ portfolio.html”页面。

I have imported User, Login class as follows: 我已经导入了User,Login类,如下所示:

   from django.http import HttpResponseRedirect
   from django.shortcuts import render
   from .models import Profile
   from django.contrib.auth.models import User
   from django.contrib.auth import authenticate, login

   # Create your views here.


   def index(request):
       return render(request, 'mysite/index.html')


   def user_login(request):
       if request.method == 'POST':
           name_r = request.POST.get('name')
           password_r = request.POST.get('password')

           user = authenticate(username=name_r, password=password_r)

           if user:
               login(request, user)

               #below line might be incorrect
               return HttpResponseRedirect('mysite/portfolio.html')
           else:
               return render(request, 'mysite/login.html')

 #rest of the code for signup which is working perfectly.

Whenever i click on Login page, the login page never loads in the first place , let alone checking whether authentication is taking place or not. 每当我单击“登录”页面时, 登录页面将永远不会加载 ,更不用说检查身份验证是否在进行了。

The error occurring is as follows: 发生的错误如下:

在此处输入图片说明

I am not sure exactly where the error is occurring and what solution must be applied to it. 我不确定确切的错误发生在哪里以及必须采取什么解决方案。

如果request.method不是POST,则您的视图永不返回任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM