登陆功能的实现:

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>
相关文章
相关标签/搜索