views:css
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
print(request.path_info)
return HttpResponse('<h1>index</h1>')
# return render(request, 'index.html')
def modal(request):
return render(request, 'modal.html')
def login(request):
# 判斷若是是get請求返回登陸頁面:
if request.method == "GET":
return render(request,"login.html")
# 不然處理post請求、獲取提交數據:
else:
print(request.POST)
#獲取用戶名:
username = request.POST.get("user")
password = request.POST.get("password")
#用戶名和密碼校驗:
if username == "alex" and password == "alexdsb":
#登陸成功:
return redirect("/index/")
else:
return render(request,"login.html",{"error":"用戶名或密碼錯誤"})
login.html:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title>{# 增長bootstrap樣式:#} <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.min.css">{# 導入css文件:#} <link rel="stylesheet" href="/static/css/signin.css"></head><body> <div class="container">{# novalidate定義不校驗、更改post請求、action提交到哪一個地址#} <form class="form-signin" method="post" action="" novalidate> <h2 class="form-signin-heading">Please sign in</h2> <label for="inputEmail" class="sr-only">用戶名</label>{# required定義必填、autofocus自動聚焦、name屬性#} <input type="text" id="inputEmail" class="form-control" name="user" placeholder="輸入用戶名" required="" autofocus=""> <label for="inputPassword" class="sr-only">密碼</label> <input type="password" id="inputPassword" class="form-control" name="password" placeholder="輸入密碼" required="">{# 提示錯誤字典的key:#} <div>{{ error }}</div> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit">登陸</button>{# 定義提交:#}{# <input type="submit" value="登陸">#} </form> </div> <!-- /container --> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script></body></html>