1. 程式人生 > >[py][mx]django get方法返回login頁面

[py][mx]django get方法返回login頁面

post gis com from imp reg form image 前端

get方法返回login.html

users/views.py

def login(request):
    if request.method == "POST":
        pass
    elif request.method == "GET":
        return render(request, "login.html")

前端請求發來request對象裏包含了method,path等.
可以debug單步調試看到
users/urls.py

from users import views

urlpatterns = [
    path('', TemplateView.as_view(template_name='index.html'), name="index"),
    path('login/', views.login, name="login"),
    path('xadmin/', xadmin.site.urls),
]

前端頁面
templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<div>
    <p><a href="/login">登錄</a></p>
    <p><a href="/register">註冊</a></p>
</div>
<script src="/static/js/jquery-3.3.1.min.js"></script>
</body>
</html>

templates/login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<div>
    <form action="/login" method="post">
        <p><input type="text" name="username" placeholder="username"></p>
        <p><input type="text" name="password" placeholder="password"></p>
        <p><input type="submit"></p>
    </form>
</div>
</body>
</html>

技術分享圖片

[py][mx]django get方法返回login頁面