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

Posted 毛台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[py][mx]django get方法返回login页面相关的知识,希望对你有一定的参考价值。

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页面的主要内容,如果未能解决你的问题,请参考以下文章

[py][mx]django实现根据城市和课程机构类别过滤

[py][mx]django处理登录逻辑

[py][mx]django自定义认证类

[py][mx]django通过邮箱找回密码

[py][mx]django模板继承-课程列表页

[py][mx]django分页第三方模块django-pure-pagination