1. 程式人生 > >django 自帶的auth認證系統

django 自帶的auth認證系統

django認證系統

版本: python2.7

django 1.8.16


project名稱:site_test, app: study

目錄結構:

-> site_test :tree
.
├── db.sqlite3
├── manage.py
├── site_test
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── static
└── templates
    ├── index.html
    ├── login.html
    ├── registration
    │   └── login.html
    └── study


1、site_test/settings文件中

INSTALL_APPS中要包含django.contrib.authdjango.contrib.contenttypes。默認就有

django.contrib.contenttypes 是 auth 模塊的用戶權限處理部分依賴的應用


MIDDLEWARE_CLASSES中要包含:

  • SessionMiddleware :用戶處理用戶會話。

  • AuthenticationMiddleware: 綁定一個 User 對象到請求中。

默認就有這兩個中間件


INSTALLED_APPS = (
    ##其他應用,
    'django.contrib.auth',
    'django.contrib.contenttypes',
   
)

MIDDLEWARE_CLASSES = (
    '其他中間件列表',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)


2、site_test/urls.py 文件

login, loginout是django自帶的方法,需要從django.contrib.auth.views中導入才能使用

from django.conf.urls import include, url
from django.contrib import admin

import views

import django.contrib.auth.views as auth_views ##不要忘記導入這個

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),  
    url(r'^login/', auth_views.login),
    url(r'^logout/', auth_views.logout_then_login),
    url(r'^$', views.index),
]

自帶的url有以下這些:

^login/$ [name='login']
^logout/$ [name='logout']
^password_change/$ [name='password_change']
^password_change/done/$ [name='password_change_done']
^password_reset/$ [name='password_reset']
^password_reset/done/$ [name='password_reset_done']
^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm']
^reset/done/$ [name='password_reset_complete']



3、site_test/views.py :

該文件定義了訪問index頁面的視圖方法,該文件需要自己創建。加上login_required() 意思是在訪問該頁面之前需要登錄


#coding:utf-8
from django.shortcuts import redirect,render

from django.contrib.auth.decorators import login_required

@login_required()
def index(request):
    return render(request, 'index.html')


login.html:

該文件位於templates/registration/login.html

註意:login.html不一定要位於registration目錄下

如果您不想調用模板registration/login.html,您可以通過額外的參數將template_name參數傳遞給as_view你的URLconf中的方法。

例如,下面URLconf中的行將使用myapp/login.html:

url(r'^accounts/login/$', auth_views.LoginView.as_view(template_name='myapp/login.html')),


login.html文件內容

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
</head>

<body>

    <form action='.' method='post'>
    {% csrf_token %}
    <label for="username">用戶名:</label>
    <input type="text" name="username" value="" id="username">
    <label for="password">密碼:</label>
    <input type="password" name="password" value="" id="password">
    <!--<input type="submit" value="login">-->
    <input type="submit" value="login">
    <input type="hidden" name="next" value="{{ next|escape }}">
  </form>

  </body>
</html>



4、settings.py中加 LOGIN_URL(未通過認證時跳轉的認證頁面) 和 LOGIN_REDIRECT_URL (登錄成功後默認跳轉的頁面)

LOGIN_URL='/login' ##登錄頁面的url

LOGIN_REDIRECT_URL='/' ##登陸後跳轉的頁面,即索引頁面


5、index.html文件

html文件中要可以直接通過{{ user }}獲取用戶名

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
{{ user }}
<a href="/logout/">退出</a>
<h1>歡迎訪問index頁面</h1>
</body>
</html>


登錄測試

技術分享圖片


註銷後返回到登錄頁面

技術分享圖片




參考文檔:http://python.usyiyi.cn/translate/django_182/topics/auth/default.html

http://blog.nsfocus.net/django-easy-steps-user-authentication/

https://www.zmrenwu.com/post/30/






django 自帶的auth認證系統