前言
基于Django的学习
详情
登录界面
找个模板
右键,检查源码
函数
def login(request):
errors = {}
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(username=username,password=password)
print("auth res",user)
if user:
print("authenticate success")
return redirect("/")
else:
errors = {"error":"用户名或者密码错误"}
return render(request,'login.html',errors) URL
url(r'^login$', views.login,name="login"), HTML
{% extends 'base.html' %} {% block body %}
<div class="container">
<div class="col-lg-3 col-lg-offset-4">
<form class="form-signin" method="POST">
<h2 class="form-signin-heading">抽屉热门社区</h2>
<label for="inputEmail" class="sr-only">用户名</label>
<input type="text" name="username" id="inputEmail" class="form-control" placeholder="username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <span style="color: red;">{{ error }}</span>
<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">Sign in</button>
</form>
</div> </div>
{% endblock %}
URL
- url(r'^login$', views.login,name="login"),
HTML
{% extends 'base.html' %} {% block body %}
<div class="container">
<div class="col-lg-3 col-lg-offset-4">
<form class="form-signin" method="POST">
<h2 class="form-signin-heading">抽屉热门社区</h2>
<label for="inputEmail" class="sr-only">用户名</label>
<input type="text" name="username" id="inputEmail" class="form-control" placeholder="username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <span style="color: red;">{{ error }}</span>
<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">Sign in</button>
</form>
</div> </div>
{% endblock %}
跳转
def index(request):
return redirect("/category/1/")