django - 登录在 django 中不起作用 用户无法登录

标签 django django-login

登录不起作用,我不知道问题出在哪里,请帮忙。

https://youtu.be/tUqUdu0Sjyc ----- Dennis Ivy -- YouTube 视频学习

浏览次数

from django.contrib.auth import authenticate, login, logout

def login(request):
    if request.method == 'post':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('/')
    return render(request, 'login.html')

html

<form method="post">
    {% csrf_token %}
    <input type="username" name="username">
    <input type="password" name="password">
    <input type="submit" name="submit">
</form>

网址

path('login/', views.login, name="login")

最佳答案

这里有两个问题:

  • request.method始终大写;和
  • View 函数和 auth 函数之间存在名称冲突。

您可以这样检查:

from django.contrib.auth import authenticate
from django.contrib.auth import <b>login as login_auth</b>
from django.contrib.auth import logout


def login(request):
    if request.method == <b>'POST'</b>:
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(request, username=username, password=password)
        if user is not None:
            <b>login_auth(</b>request, user<b>)</b>
            return redirect('/')
    return render(request, 'login.html')

关于django - 登录在 django 中不起作用 用户无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75208546/

相关文章:

django - 如何在 Django 模板中更新由 'with' 设置的变量

python - Docker 开发环境,pylint 无法导入错误

python - 无法在 django 中将单选按钮设置为必需

Django - 自定义身份验证后端中的错误消息

django - "name ' django ' not defined"使用通用登录 View 时出错?

python - Django Forbidden 403 Error "CSRF token missing or incorrect"已登录时尝试登录

python - 获取 检查 'apps.ChatbotConfig.name' 是否正确。重组后

python - 如何在 Django 管理中显示 auto_now_add 字段?

python - 使用 django-axes 登录 Django

django - 如何在登录失败后清除密码字段?