首页 > 代码库 > 一个初学者的辛酸路程-基于Django写BBS项目

一个初学者的辛酸路程-基于Django写BBS项目

前言

基于Django的学习

详情

登录界面

找个模板
http://v3.bootcss.com/examples/signin/
技术分享
右键,检查源码
技术分享
 
 
函数
 
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=http://www.mamicode.com/"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
  1. url(r‘^login$‘, views.login,name="login"),
 
HTML
  1. {% 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=http://www.mamicode.com/"remember-me"> Remember me
                      </label>
                    </div>
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
                </form>
            </div>
     
        </div>
    {% endblock %}

     

 
跳转
  1. def index(request):
        return redirect("/category/1/")

一个初学者的辛酸路程-基于Django写BBS项目