找不到页面(404) 请求方式:POST 请求网址:http://127.0.0.1:8000/login/login/

问题描述 投票:0回答:1

我将用户注册详细信息存储在数据库中,但是当我尝试使用输入的数据登录时,它不起作用;相反,它会显示以下消息。

enter image description here

这里是urls.py

urlpatterns=[

    path('',views.index,name='index'),
    #path('counter',views.counter,name='counter'),
    path('register/',views.register,name='register'),
    path('login/',views.login,name='login.html'),
    path('logout/',views.logout,name='logout.html'),
    path('post/<str:pk>',views.post,name='post.html')
      #path('',views.display_records,name='table.html'),
]

这里是views.py

def login(request):
    if request.method=='POST':
        username=request.POST['username']
        password=request.POST['password']
        user=auth.authenticate(username=username,password=password)
        if user is not None:
            auth.login(request,user)
            return redirect('/')
        else:
            messages.info(request,'Invalid User Login')
            return redirect('login')
    else:
        return render(request,'login.html')
    

这里是login.html

<h1>Login Now</h1>
<style>
    h3{
        color: red;
    }
</style>
{% for message in messages %}
<h3>{{message}}</h3>
{% endfor %}
<form action="login/" method='POST'>
    {% csrf_token %}
    <p>Username</p>
    <input type="text" name="username" placeholder="username"/>
    <p>Password</p>
    <input type="password" name="password" placeholder="password"/>
    <br>
    <br>

    <input type="submit"/>
</form>

注册完成后,它不会重定向到登录页面,而是显示此页面。

type here

enter image description here

python django authentication localhost
1个回答
0
投票

在您定义的网址/路径中,只有 /login/login /login

请尝试将表单中的操作留空:

从此:

<form action="login/" method='POST'>

对此:

<form action="" method='POST'>
© www.soinside.com 2019 - 2024. All rights reserved.