手撸系列1完整的登录功能

1.完整的登录功能

笔记

1 login.html  
        ***重点***1 action:提交到后台的地址三种写法:
                1 http://127.0.0.1:8000/login
                2 /login/   推荐用
                32 method  post方式
              3 <input type="submit" value="提交">或<button></button>
                type不可以是button
           <form action="http://127.0.0.1:8000/login" method="post">
            <p>用户名:<input type="text" name="name" class="form-control"></p>
            <p >
                密码:<input type="password" name="pwd" class="form-control">
            </p>
            <input type="submit" value="提交">
        </form>
    2 视图层:
        1 request.method  ----前台提交过来请求的方式
        2 request.POST(相当于字典)----post形式提交过来的数据,(http请求报文的请求体重)
        3 request.POST.get('name') ----推荐用get取值(取出列表最后一个值)
        4 request.POST.getlist('name')-----取出列表所有的值_
        5 前台get方式提交的数据,从request.GET字典里取
    3 链接数据库(防止注入,推荐以下写法)
        cur.execute('select * from user where name=%s and password=%s ',[name,pwd])

post请求

login.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/css/bootstrap-3.3.7-dist/css/bootstrap.css">
{#    注意导入css文件路径的正确,后台可以用console查看#}
    <link rel="stylesheet" href="/static/mycss.css">
    <title>登陆</title>
</head>
<body>
<h1>登录</h1>
<div class="col-md-6 col-md-offset-3">
{#    <form action="http://127.0.0.1:8000/hello" method="post">#}
    <form action="/hello/" method="post">
        <p>用户名1:<input type="text" name="name" class="form-control"></p>
        <p>用户名2:<input type="text" name="name" class="form-control"></p>
        <p>密码:<input type="password" name="pwd" class="form-control"></p>
{#        用户名1,用户名2,密码打包发到请求体里面#}
        <input type="submit" value="提交">
    </form>
</div>
</body>
</html>

记得setting文件的静态配置

urls文件

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('hello',views.loginhtml),
]

views文件

from django.shortcuts import render,HttpResponse
import pymysql

def loginhtml(request):
    print('提交')
    #大写
    print(request.method)
    if request.method=="GET":
        return render(request,'login.html')
    elif request.method=="POST":#从请求体里面拿出来
        print(request.POST)
        print('========')
        name=request.POST.get('name')
        # 取的是后面一个
        pwd=request.POST.get('pwd')
        l1_name=request.POST.getlist('name')#取多个值用getlist
        print(type(request.POST))
        print(l1_name)
        from django.http.request import QueryDict
        print(name)
        conn=pymysql.connect(host="127.0.0.1",port=3306,db="",password="")#取完之后连接数据库做查询
        cur=conn.cursor()
        cur.execute('select*from user where name=%s and password=%s',[name,pwd])

 get请求

login.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/css/bootstrap-3.3.7-dist/css/bootstrap.css">
{#    注意导入css文件路径的正确,后台可以用console查看#}
    <link rel="stylesheet" href="/static/mycss.css">
    <title>登陆</title>
</head>
<body>
<h1>登录</h1>
<div class="col-md-6 col-md-offset-3">
{#    <form action="http://127.0.0.1:8000/hello" method="post">#}
    <form action="/getqingqiu/" method="get">
{#        action=这个是路径#}
        <p>用户名1:<input type="text" name="name" class="form-control"></p>
        <p>用户名2:<input type="text" name="name" class="form-control"></p>
        <p>密码:<input type="password" name="pwd" class="form-control"></p>
{#        用户名1,用户名2,密码打包发到请求体里面#}
        <input type="submit" value="提交">
    </form>
</div>
</body>
</html>

urls文件

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('hello',views.loginhtml),
    url('getqingqiu',views.login2)
]

views文件

from django.shortcuts import render,HttpResponse
import pymysql

def loginhtml(request):
    print('提交')
    #大写
    print(request.method)
    if request.method=="GET":
        return render(request,'login.html')
    elif request.method=="POST":#从请求体里面拿出来
        print(request.POST)
        print('========')
        name=request.POST.get('name')
        # 取的是后面一个
        pwd=request.POST.get('pwd')
        l1_name=request.POST.getlist('name')#取多个值用getlist
        print(type(request.POST))
        print(l1_name)
        from django.http.request import QueryDict
        print(name)
        conn=pymysql.connect(host="127.0.0.1",port=3306,db="",password="")#取完之后连接数据库做查询
        cur=conn.cursor()
        cur.execute('select*from user where name=%s and password=%s',[name,pwd])

def login2(request):
    if request.method=="GET":
        print(request.GET)

    return HttpResponse("OK")

 

posted @ 2019-03-07 15:02  王苗鲁  阅读(638)  评论(0编辑  收藏  举报