18-crm项目-kingadmin,完成crm用户认证登陆

完成crm用户认证登陆

{% extends 'base.html' %}

{% block body %}
    <div class="container col-lg-offset-4 ">
      <form class="form-signin col-lg-3 pu " method="post"  role="form">{% csrf_token %}
        <h3 class="form-signin-heading">Oldboy PerfectCRM</h3>
        <input type="email"  name="email" class="form-control" placeholder="Email address" required autofocus>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
        {% if errors %}
           <span style="color: red;">{{ errors.error }}</span>
        {% endif %}
        <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">Login</button>
      </form>

    </div> <!-- /container -->

{% endblock %}

 

增加url路由

from django.conf.urls import url,include
from django.contrib import admin
# admin.autodiscover()
from PerfectCRM import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^crm/', include("crm.urls")),
    url(r'^$', views.index),
    url(r'^account/login/$', views.acc_login),
    url(r'^account/logout/$', views.acc_logout,name="acc_logout"),
    url(r'^student/', include("student.urls")),
    url(r'^king_admin/', include("king_admin.urls")),
]

 

增加视图:

def acc_login(request):

    errors = {}
    if request.method == "POST":
        _email = request.POST.get("email")
        _password = request.POST.get("password")

        user = authenticate(username = _email, password = _password)
        if user:
            login(request,user)
            next_url = request.GET.get("next","/")
            return redirect(next_url)
        else:
            errors['error'] = "Wrong username or password!"


    return render(request,"login.html",{"errors":errors})

 

对每一个视图进行登陆校验,使用了django自带的认证系统,

from django.contrib.auth.decorators import login_required

 

还需要一个登出

def acc_logout(request):

    logout(request)

    return redirect("/account/login/")

 

 

 

 

 

 

###################################

posted @ 2020-10-10 19:20  技术改变命运Andy  阅读(401)  评论(0编辑  收藏  举报