找不到页面(404)请求方法:POST请求URL:http://127.0.0.1:8000/login/login1

时间:2020-05-25 06:37:57

标签: python django authentication django-templates

当我登录到“登录”页面时,浏览器显示此错误:

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

Django使用webapp.urls中定义的URLconf,按以下顺序尝试了以下URL模式:

  1. admin /
  2. 首页/ [name ='首页']
  3. 登录名/ [name ='login']
  4. 注册/ [name ='Signup']
  5. login1 / [name ='login1']

当前路径login / login1与任何这些都不匹配。

这是login.html文件:

<body>
    <center> <h1> Student Login Form </h1> </center>
    <form method="post" action='login1'>       
        {% csrf_token %}       
        <h1 style="color:red"> {{ error}} </h1>        
        <div class="container">            
            <label>Username : </label>            
            <input type="text" placeholder="Enter Username" name="username" required>            
            <label>Password : </label>            
            <input type="password" placeholder="Enter Password" name="pass" required>            
            <button type="submit">Login</button>            
            <input type="checkbox" checked="checked"> Remember me            
            <button type="button" class="cancelbtn"> Cancel</button>            
            <a href="{% url 'Signup' %}"> SignUp </a>       
        </div>    
    </form>
</body>

这是urls.py:

from django.contrib import admin
from django.urls import path
from application import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('Home/', views.Home , name = "Home"),
    path('login/', views.login,name = 'login'),
    path('Signup/', views.Signup , name = 'Signup'),
    path('login1/',views.login1,name='login1'),
]

这是views.py:

def login(request):
    return render(request, 'login.html')

def login1(request):
    if request.method == "POST":
        uname = request.POST['username']
        pwd = request.POST['pass']
        user = auth.authenticate(username=uname, password=pwd)
        if user is not None:
            auth.login(request, user)
            return render(request, 'Home.html')
        else:
            return render(request, 'login.html', {'error': 'invalid credential details'})
    else:
        return render(request, 'Signup.html')

1 个答案:

答案 0 :(得分:1)

问题可能在这一行:

<form method="post" action='login1'>

更好地以这种方式写入action属性的值。

<form method="post" action="{% url 'login1' %}">