日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

django用户认证系统——登录4

發布時間:2023/12/1 windows 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django用户认证系统——登录4 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

用戶已經能夠在我們的網站注冊了,注冊就是為了登錄,接下來我們為用戶提供登錄功能。和注冊不同的是,Django 已經為我們寫好了登錄功能的全部代碼,我們不必像之前處理注冊流程那樣費勁了。只需幾分鐘的簡單配置,就可為用戶提供登錄功能。接下來就來看看如何使用內置的登錄功能。

引入內置的 URL 模型

Django 內置的登錄、修改密碼、找回密碼等視圖函數對應的 URL 模式位于 django.contrib.auth.urls.py 中,首先在工程的 urls.py 文件里包含這些 URL 模式。打開 django_auth_example/ 目錄下的 urls.py 文件,將 django.contrib.auth.urls.py 包含進來:

django_auth_example/urls.pyfrom django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^users/', include('users.urls')), # 將 auth 應用中的 urls 模塊包含進來 url(r'^users/', include('django.contrib.auth.urls')), ]

這將包含以下的 URL 模式:

^users/login/$ [name='login'] ^users/logout/$ [name='logout'] ^users/password_change/$ [name='password_change'] ^users/password_change/done/$ [name='password_change_done'] ^users/password_reset/$ [name='password_reset'] ^users/password_reset/done/$ [name='password_reset_done'] ^users/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'] ^users/reset/done/$ [name='password_reset_complete']

設置模板路徑

默認的登錄視圖函數渲染的是 registration/login.html 模板,因此需要在 templates/ 目錄下新建一個 registration 文件夾,再在 registration/ 目錄下新建 login.html 模板文件。此時目錄結構為:

django_auth_example/manage.pydjango_auth_example/__init__.pysettings.pyurls.pywsgi.pytemplates/users/register.htmlregistration/login.html

編寫登錄模板

登錄模板的代碼和注冊模板的代碼十分類似:

registration/login.html<!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.0, maximum-scale=1.0, user-scalable=no"> <title>登錄</title> <link rel="stylesheet" href="https://unpkg.com/mobi.css/dist/mobi.min.css"> <style> .errorlist { color: red; } </style> </head> <body> <div class="flex-center"> <div class="container"> <div class="flex-center"> <div class="unit-1-2 unit-1-on-mobile"> <h3>登錄</h3> <form class="form" action="{% url 'login' %}" method="post"> {% csrf_token %} {{ form.non_field_errors }} {% for field in form %} {{ field.label_tag }} {{ field }} {{ field.errors }} {% if field.help_text %} <p class="help text-small text-muted">{{ field.help_text|safe }}</p> {% endif %} {% endfor %} <button type="submit" class="btn btn-primary btn-block">登錄</button> </form> <div class="flex-left top-gap text-small"> <div class="unit-2-3"><span>沒有賬號?<a href="{% url 'users:register' %}">立即注冊</a></span></div> <div class="unit-1-3 flex-right"><span><a href="reset_password.html">忘記密碼?</a></span></div> </div> </div> </div> </div> </div> </body> </html>

為了登錄頁面的美觀我引入了 mobi.css 提供樣式支持,其它代碼請忽略,我們只關注表單部分的代碼。

<form class="form" action="{% url 'login' %}" method="post"> {% csrf_token %} {{ form.non_field_errors }} {% for field in form %} {{ field.label_tag }} {{ field }} {{ field.errors }} {% if field.help_text %} <p class="help text-small text-muted">{{ field.help_text|safe }}</p> {% endif %} {% endfor %} <button type="submit" class="btn btn-primary btn-block">登錄</button> </form>

循環表單字段、渲染控件、渲染幫助信息等在注冊表單部分已經講過,登錄表單中只引入了一個新的東西:{{ form.non_field_errors }},這顯示的同樣是表單錯誤,但是顯示的表單錯誤是和具體的某個表單字段無關的。相對 {{ field.errors }},這個則顯示的是具體某個字段的錯誤。比如對于字段 username,如果用戶輸入的 username 不符合要求,比如太長了或者太短了,表單會在 username 下方渲染這個錯誤。但有些表單錯誤不和任何具體的字段相關,比如用戶輸入的用戶名和密碼無法通過驗證,這可能是用戶輸入的用戶名不存在,也可能是用戶輸入的密碼錯誤,因此這個錯誤信息將通過 {{ form.non_field_errors }} 渲染。

注意:你可能覺得用戶名不存在錯誤和 username 字段有關,密碼錯誤和 password 字段有關。但是在現代的用戶認證系統中,我們不為用戶提供這么詳細的信息,只是籠統地告知用戶名不存在或者密碼錯誤。這能提高一些用戶賬戶的安全性。

此外登錄表單的 action 屬性的值是?{% url 'login' %},即 auth 應用下的?login?視圖函數對應的 URL,用戶提交的表單數據將提交給這個 URL,Django 調用?login?視圖函數來處理。

不要忘了加 {% csrf_token %} 模板標簽。

現在打開開發服務器,在瀏覽器輸入 http://127.0.0.1:8000/users/login/,你將看到一個用戶登陸表單。

故意使用一個不存在的賬戶登錄,或者故意輸錯密碼,你將看到表單渲染的非字段相關的錯誤。

如果用戶登錄成功,你會發現跳轉到了 http://127.0.0.1:8000/accounts/profile/ 頁面。由于我們沒有寫任何視圖函數處理這個 URL,所以看到一個 404 錯誤。不過沒有關系,我們目前只關注用戶是否已經登錄。

如何在模板中判斷用戶是否已經登錄

在模板中判斷用戶是否已經登錄非常簡單,使用 {% if user.is_authenticated %} 條件判斷即可。借此機會,我們來處理一下網站首頁。

在 user/views.py 寫一個首頁視圖函數:

user/views.py def index(request): return render(request, 'index.html')

為這個視圖函數配置 URL 模式,在 django_auth_example/urls.py 進行配置:

from django.conf.urls import url, include from django.contrib import admin from users import views urlpatterns = [ url(r'^admin/', admin.site.urls), # 別忘記在頂部引入 include 函數 url(r'^users/', include('users.urls')), url(r'^users/', include('django.contrib.auth.urls')), # 別忘記在頂部引入 views 模塊 url(r'^$', views.index, name='index') ]

注意:直接在項目的 urls.py 中配置 URL 是不推薦的,應該在應用的 urls.py 下進行配置,然后在項目的 urls.py 中通過?include?函數包含。不過這里作為示例情況特殊,所以姑且這樣做。

由于視圖渲染了 index.html 文件,因此在 templates/ 目錄下建一個 index.html 模板文件(注意我們沒有把它放在 users/ 下,也沒放在 registration/ 下)。然后寫上下面的代碼:

templates/index.html<!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.0, maximum-scale=1.0, user-scalable=no"> <title>首頁</title> <link rel="stylesheet" href="https://unpkg.com/mobi.css/dist/mobi.min.css"> </head> <body> <div class="flex-center"> <div class="container"> <div> <h1 class="logo"><a href="{% url 'index' %}">Django Auth Example</a></h1> {% if user.is_authenticated %} <p>你已登錄,歡迎你:<a href="#">{{ user.username }}</a></p> {% else %} <p>你還沒有登錄,請 <button class="btn btn-default"><a href="{% url 'login' %}">登錄</a></button> 或者 <button class="btn btn-default"><a href="{% url 'users:register' %}">注冊</a></button> </p> {% endif %} </div> </div> </div> </body> </html>

為了頁面的美觀,我引入了 mobi.css 提供樣式支持。其它代碼請忽略,重點只關注用戶登錄驗證部分:

{% if user.is_authenticated %}<p>你已登錄,歡迎你:<a href="#">{{ user.username }}</a></p> {% else %} <p>你還沒有登錄,請 <button class="btn btn-default"><a href="{% url 'login' %}">登錄</a></button> 或者 <button class="btn btn-default"><a href="{% url 'users:register' %}">注冊</a></button> </p> {% endif %}

user.is_authenticated?當用戶已經登錄時返回 True,否則返回 False。所以已登錄的用戶將看到歡迎頁面,否則將看到登錄注冊按鈕。

你也許奇怪我們在?index?視圖中并沒有傳遞?user?模板變量給 index.html,為什么可以在模板中引用 user 呢?這是因為 Django 的 auth 應用為我們設置了模板常量,所以在任何模板中都可以引用 {{ user }}。此外,我們之前提過的 django.contrib.auth.middleware.AuthenticationMiddleware 為所有的請求 request 綁定了一個 user 屬性。所以在模板中引用 {{ user }} 和 {{ request.user }} 是等價。

OK 了!不過目前為止,如果你已經登錄過了,想要看看未登錄的效果會變得比較困難,因為我們還無法注銷登錄。下面就來給網站添加注銷登錄的功能吧!

轉載于:https://www.cnblogs.com/AmilyWilly/p/8469859.html

總結

以上是生活随笔為你收集整理的django用户认证系统——登录4的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。