django 認證登陸

AbstractUser
from django.contrib.auth.models import AbstractUser AbstractBaseUser中只含有3個field: password, last_login和is_active. 這個就是你本身高度定製本身須要的東西css

什麼是基於擴展AbstractBaseUser來建立的自定義用戶模型?html

它是繼承於AbstractBaseUser類的一個全新的用戶系統。這須要經過settings.py進行特別的維護和更新一些相關的文件。理想的狀況是,它在項目開始時就已經被設計,由於它對數據庫架構影響很大。在執行時都須要額外的維護。數據庫

何時須要擴展AbstractBaseUser類自定義用戶模型呢?django

當應用對驗證過程具備特殊要求時,那麼須要自定義一個用戶模型。好比,在一些將郵件地址代替用戶名做爲驗證令牌的狀況時,自定義用戶模型更合適。session

方法4:擴展AbstractUser來建立自定義用戶模型架構

什麼是基於擴展AbstractUser建立的自定義用戶模型?app

它是繼承於AbstractUser類的一個全新的用戶系統。它也須要經過settings.py進行特別的維護和更新一些相關的文件。理想的狀況是,它在項目開始以前就已經被設計,由於它對數據庫架構影響很大。在執行時須要額外的維護。框架

何時須要擴展AbstractBaseUser類的自定義用戶模型呢?函數

當你對Django處理身份驗證過程很滿意而又不會改動它時,那可使用它。或者,你想直接在用戶模型中增長一些額外的信息而不想建立新的類時,也可使用它。
is_staff
是否容許user訪問admin界面post

is_active
用戶是否活躍。

1、重寫user,將新的user註冊到admin,還要重寫認證
2、繼承user,進行擴展(記得在settings中設置AUTH_USER_MODEL

settings.py 設置AUTH_USER_MODEL = "myapp.NewUser"

# Custom User Auth model AUTH_USER_MODEL = 'users.User'

2.1 繼承AbstractUser類

from django.contrib.auth.models import AbstractUser from django.utils.translation import ugettext_lazy as _ from django.db import models from django.conf import settings from . import UserGroup __all__ = ['User'] class User(AbstractUser): ROLE_CHOICES = ( ('Admin', _('Administrator')), ('User', _('User')), ('App', _('Application')) ) username = models.CharField(max_length=20, unique=True, verbose_name=_('Username')) name = models.CharField(max_length=20, verbose_name=_('Name')) email = models.EmailField(max_length=30, unique=True, verbose_name=_('Email')) groups = models.ManyToManyField(UserGroup, related_name='users', blank=True, verbose_name=_('User group')) role = models.CharField(choices=ROLE_CHOICES, default='User', max_length=10, blank=True, verbose_name=_('Role')) is_first_login = models.BooleanField(default=False) comment = models.TextField(max_length=200, blank=True, verbose_name=_('Comment')) created_by = models.CharField(max_length=30, default='', verbose_name=_('Created by'))  @property def is_superuser(self): if self.role == 'Admin': return True else: return False  @is_superuser.setter def is_superuser(self, value): if value is True: self.role = 'Admin' else: self.role = 'User'  @property def is_staff(self): if self.is_authenticated and self.is_valid: return True else: return False  @is_staff.setter def is_staff(self, value): pass

踩過的坑:
一、登錄的時候用自帶的認證模塊老是報none

user = authenticate(username=username, password=password)

查看源碼發現是check_password的方法是用hash進行校驗,以前註冊的password寫法是

user.password=password

這種寫法是明文入庫,須要更改密碼的入庫寫法

user.set_password(password)

form 設置

from django import forms from django.contrib.auth.forms import AuthenticationForm from django.utils.translation import gettext_lazy as _ class UserLoginForm(AuthenticationForm): username = forms.CharField(label=_('Username'), max_length=100) password = forms.CharField( label=_('Password'), widget=forms.PasswordInput, max_length=100, strip=False)

AuthenticationForm登陸表單
用於用戶登陸的表單。
默認狀況下,AuthenticationForm 將拒絕is_active 標誌爲False 的用戶。

login.py配置

from django.shortcuts import render,HttpResponse from django.shortcuts import reverse, redirect from django.contrib.auth import login as auth_login, logout as auth_logout from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_protect from django.views.generic.edit import FormView from .. import forms from django.views.decorators.cache import never_cache from django.views.decorators.debug import sensitive_post_parameters @method_decorator(csrf_protect, name='dispatch') class UserLoginView(FormView): template_name = 'users/login.html' form_class = forms.UserLoginForm redirect_field_name = 'next' def get(self, request, *args, **kwargs): if request.user.is_staff: return redirect(self.get_success_url()) return super(UserLoginView, self).get(request, *args, **kwargs) def form_valid(self, form): auth_login(self.request, form.get_user()) return redirect(self.get_success_url()) def get_success_url(self): if self.request.user.is_first_login: return reverse('users:user-first-login') return self.request.POST.get( self.redirect_field_name, self.request.GET.get(self.redirect_field_name, reverse('index')))

登錄login

login()

登錄函數,須要一個HttpRequest對象和一個User對象做參數。login()使用django的session框架,將User的id存儲在session中。

同時使用authenticate()和login():

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) # Redirect to a success page. else: # Return a 'disabled account' error message else: # Return an 'invalid login' error message.

若是不知道密碼,login方法:

user=MyUser.objects.get(...)
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request,user)

FormView使用基於類的視圖處理表單 表單的處理一般有3 個步驟:

  • 初始的GET (空白或預填充的表單)
  • 帶有非法數據的POST(一般從新顯示錶單和錯誤信息)
  • 帶有合法數據的POST(處理數據並重定向) 使用FormView 來構造其視圖:
from myapp.forms import ContactForm from django.views.generic.edit import FormView class ContactView(FormView): template_name = 'contact.html' form_class = ContactForm success_url = '/thanks/' def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. form.send_email() return super(ContactView, self).form_valid(form)

html設置:

{% load static %} {% load i18n %} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JumpServer </title> <link rel="shortcut icon" href="{% static "img/facio.ico" %}" type="image/x-icon"> {% include '_head_css_js.html' %} <link href="{% static "css/jumpserver.css" %}" rel="stylesheet"> <script src="{% static "js/jumpserver.js" %}"></script> <style> .captcha { float: right; } </style> </head> <body class="gray-bg"> <div class="loginColumns animated fadeInDown"> <div class="row"> <div class="col-md-6"> </div> <div class="col-md-6"> <div class="ibox-content"> <div><img src="{% static 'img/logo.png' %}" width="82" height="82"> <span class="font-bold text-center" style="font-size: 32px; font-family: inherit">{% trans 'Login' %}</span></div> <form class="m-t" role="form" method="post" action=""> {% csrf_token %} {% if form.errors %} {% if 'captcha' in form.errors %} <p class="red-fonts">{% trans 'Captcha invalid' %}</p> {% else %} <p class="red-fonts">{{ form.non_field_errors.as_text }}</p> {% endif %} {% endif %} <div class="form-group"> <input type="text" class="form-control" name="{{ form.username.html_name }}" placeholder="{% trans 'Username' %}" required=""> </div> <div class="form-group"> <input type="password" class="form-control" name="{{ form.password.html_name }}" placeholder="{% trans 'Password' %}" required=""> </div> <div> {# {{ form.captcha }}#} </div> <button type="submit" class="btn btn-primary block full-width m-b">{% trans 'Login' %}</button> {# <a href="{% url 'users:forgot-password' %}">#} <small>{% trans 'Forgot password' %}?</small> </a> <p class="text-muted text-center"> </p> </form> <p class="m-t"> </p> </div> </div> </div> <hr/> <div class="row"> <div class="col-md-6"> Copyright Jumpserver.org </div> <div class="col-md-6 text-right"> <small>© 2014-2017</small> </div> </div> </div> </body> </html>

生成表信息以下:

id	password	last_login	first_name	last_name	is_active	data_joined	username	name	email	role	comment	created_bt	is_first_login
1	pbkdf2_sha256$30000$f8HLJLRwydBR$M+X1huSgQOojfaG01SGNBYPOlbYnHHw/A4/RdcSBEQQ= 11/6/2017 18:55:52 1 1 11/5/2017 18:56:05 admin Administrator a
相關文章
相關標籤/搜索