Django框架学习笔记(2.实现用户登录(上))

来源:互联网 发布:川普 好莱坞 知乎 编辑:程序博客网 时间:2024/06/06 05:22

先写简单的:

views.py:

from django.shortcuts import HttpResponsefrom django.shortcuts import renderdef login(request):    return render(request, "login.html")def home(request):    return HttpResponse("<h1>你好</h1>")
urls.py:

from django.shortcuts import HttpResponsefrom django.shortcuts import renderdef login(request):    return render(request, "login.html")def home(request):    return HttpResponse("<h1>你好</h1>")
templates下建立login.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <style>        label{            width: 80px;            text-align: right;            display: inline-block;        }    </style></head><body><form action="/login" method="get">    <p>        <label for="username">用户名</label>        <input id="username" type='text'/>    </p>    <p>        <label for="password">密码</label>        <input id="password" type='text'/>        <input type="submit" value="提交"/>    </p></form></body></html>


运行:



如果有CSS和JS(静态文件),会出错,那么接下来配置:

创建了一个static文件夹,放入JS和CSS文件,然后在HTML文件中修改下:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link rel="stylesheet" href="/static/commons.css">    <style>        label{            width: 80px;            text-align: right;            display: inline-block;        }    </style></head><body><form action="/login" method="get">    <p>        <label for="username">用户名</label>        <input id="username" type='text'/>    </p>    <p>        <label for="password">密码</label>        <input id="password" type='text'/>        <input type="submit" value="提交"/>    </p></form><script src="/static/jquery.min.js"></script></body></html>

在settings.py后面加上这段:

STATICFILES_DIRS={    os.path.join(BASE_DIR, "static"),}

再次运行:



由此可以得出:

应该优先

1.配置模板路径(在settings.py的TEMPLATES里的‘DIRS’)

2.配置静态目录(见上)



静态文件处理好了,那么点击提交后到底提交到了哪里呢?

继续修改,完成用户名密码验证:

注释掉setting.py里面的:

MIDDLEWARE = [    'django.middleware.security.SecurityMiddleware',    'django.contrib.sessions.middleware.SessionMiddleware',    'django.middleware.common.CommonMiddleware',    #'django.middleware.csrf.CsrfViewMiddleware',    'django.contrib.auth.middleware.AuthenticationMiddleware',    'django.contrib.messages.middleware.MessageMiddleware',    'django.middleware.clickjacking.XFrameOptionsMiddleware',]

views.py修改如下:

from django.shortcuts import HttpResponsefrom django.shortcuts import renderfrom django.shortcuts import redirectdef login(request):    error_msg = ""    if request.method == "POST":        user = request.POST.get("user", None)        pwd = request.POST.get("pwd", None)        if user == "yiqing" and pwd == "handsome":            return redirect("http://www.baidu.com")        else:            error_msg = "用户名或密码错误"    return render(request, "login.html", {"error_msg": error_msg})def home(request):    return HttpResponse("<h1>你好</h1>")

login.html修改如下:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link rel="stylesheet" href="/static/commons.css">    <style>        label{            width: 80px;            text-align: right;            display: inline-block;        }    </style></head><body><form action="/login/" method="POST">    <p>        <label for="username">用户名</label>        <input id="username" type='text' name="user"/>    </p>    <p>        <label for="password">密码</label>        <input id="password" type='password' name="pwd"/>        <input type="submit" value="提交"/>        <span style="color:red">{{ error_msg }}</span>    </p></form><script src="/static/jquery.min.js"></script></body></html>

如果输入用户名和密码正确,则会跳转百度,否则告诉用户错误:



阅读全文
0 0
原创粉丝点击