django admin 默認已經存在了一個用戶認證,這個時候能夠偷個小懶,直接用 django 自帶的,就不須要本身寫用戶認證了css
一、目錄結構:html
二、代碼 python
# 若是不添加該行,則在未登陸狀態打開頁面的時候驗證是否登陸的裝飾器跳轉到 /accounts/login/ 下面
# 第一種解決方法就是修改 settings.py 中的 LOGIN_URL
# 第二種解決方法是在 url 中匹配該 url
LOGIN_URL = "/login/"
from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): """帳戶信息表""" user = models.OneToOneField(User) # 跟 django 的 User 表作一個一對一 name = models.CharField(max_length=32) roles = models.ManyToManyField("Role", blank=True, null=True)
from django.conf.urls import url, include from django.contrib import admin from CRM import views urlpatterns = [ url(r'^crm/', include("app01.urls")), url(r'^login/', views.acc_login), url(r'^logout/', views.acc_logout), ]
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author:zhangcong # Email:zc_92@sina.com from django.contrib.auth import authenticate, login, logout from django.shortcuts import render from django.shortcuts import redirect def acc_login(request): """登陸""" print(request.user) if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") user = authenticate(username=username, password=password) # 調用 django 的認證模塊進行認證 if user: # 判斷驗證是否經過 login(request, user) next_url = request.GET.get('next', None) if not next_url: next_url = '/crm/' return redirect(next_url) return render(request, 'login.html') def acc_logout(request): """登出""" logout(request) return redirect('/login/')
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! --> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="http://v3.bootcss.com/favicon.ico"> <title>Signin Template for Bootstrap</title> <!-- Bootstrap core CSS --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> <link href="http://v3.bootcss.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="http://v3.bootcss.com/examples/signin/signin.css" rel="stylesheet"> <style> .line{ margin-top: 5px; } </style> </head> <body> <div class="container"> <form class="form-signin" method="POST"> {% csrf_token %} <h2 class="form-signin-heading">Please sign in</h2> <div class="line"> <label for="username" class="sr-only">UserName</label> <input type="text" id="username" class="form-control" name="username" placeholder="UserName" required autofocus> </div> <div class="line"> <label for="inputPassword" class="sr-only">Password</label> <input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password" required> </div> <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> </form> </div> <!-- /container --> </body> </html>
from django.conf.urls import url, include from app01 import views urlpatterns = [ url(r'^$', views.dashboard), ]
from django.shortcuts import HttpResponse from django.contrib.auth.decorators import login_required @login_required # 驗證是否登陸 def dashboard(request): print(request.user) return HttpResponse('login ok!')