url(r'^login', views2.login), url(r'^index', views2.index), url(r'^test', views.test),
導入pymysql模塊css
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('用戶名或密碼錯誤')
<!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>
<!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>同城交友</p> <p><a class="btn btn-primary btn-lg" href="#" role="button">瘋狂點擊我</a></p> </div> </div> <div class="container"> <!-- Example row of columns --> <div class="row"> <div class="col-md-4"> <h2>Heading</h2> <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p> <p><a class="btn btn-default" href="#" role="button">View details »</a></p> </div> <div class="col-md-4"> <h2>Heading</h2> <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p> <p><a class="btn btn-default" href="#" role="button">View details »</a></p> </div> <div class="col-md-4"> <h2>Heading</h2> <p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p> <p><a class="btn btn-default" href="#" role="button">View details »</a></p> </div> </div> <hr> <footer> <p>© 2016 Company, Inc.</p> </footer> </div> <!-- /container --> </body> </html>