django框架之请求生命周期及ORM引入等相关内容-61

1 登录功能,连接mysql

路由

    url(r'^login', views2.login),
   url(r'^index', views2.index),
   url(r'^test', views.test),

视图函数

def index(request):
   return render(request, 'index2.html')


def login(request):
   if request.method == 'GET':
       return render(request, 'login.html')
   else:
       name = request.POST.get('name')
       password = request.POST.get('password')
       # 创建一个数据库链接
       conn = pymysql.connect(host='127.0.0.1', user='root', password='123', database='userinfo', port=3306, )
       # 拿到一个游标
       cursor = conn.cursor()
       # 执行sql
       cursor.execute('select * from user where name=%s and password=%s ', (name, password))
       # 获取结果
       ret = cursor.fetchone()
       print(ret)
       if ret:
           return redirect('/index')
       else:
           return HttpResponse('用户名或密码错误')

模板文件login.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
   <title>Title</title>
</head>
<body>

<div class="row">
   <div class="col-md-6 col-md-offset-3">
       <h1 class="text-center">登录功能</h1>
       <form action="" method="post">
           <div class="form-group">
               <label for="">用户名:</label>
               <input type="text" name="name" class="form-control">
           </div>


           <div class="form-group">
               <label for="">密码:</label>
               <input type="password" name="password" class="form-control">
           </div>

           <div class="text-center">

               <input type="submit" value="登录" class="btn btn-success">
           </div>

       </form>
   </div>

</div>

</body>
</html>

模板文件index2.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
   <script src="/static/js/jquery.min.js"></script>
   <title>首页</title>
</head>
<body>

   <nav class="navbar navbar-inverse navbar-fixed-top">
     <div class="container">
       <div class="navbar-header">
         <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
           <span class="sr-only">导航</span>
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
         </button>
         <a class="navbar-brand" href="#">Project name</a>
       </div>
       <div id="navbar" class="navbar-collapse collapse">
         <form class="navbar-form navbar-right">
           <div class="form-group">
             <input type="text" placeholder="Email" class="form-control">
           </div>
           <div class="form-group">
             <input type="password" placeholder="Password" class="form-control">
           </div>
           <button type="submit" class="btn btn-success">Sign in</button>
         </form>
       </div><!--/.navbar-collapse -->
     </div>
   </nav>

   <!-- Main jumbotron for a primary marketing message or call to action -->
   <div class="jumbotron">
     <div class="container">
       <h1>刘清政</h1>
       <p>同城交友</