Django框架使用內(nèi)置方法實(shí)現(xiàn)登錄功能詳解
本文實(shí)例講述了Django框架使用內(nèi)置方法實(shí)現(xiàn)登錄功能。分享給大家供大家參考,具體如下:
一 內(nèi)置登錄退出思維導(dǎo)圖

二 Django內(nèi)置登錄方法
1 位置

2 源碼
@deprecate_current_app
@sensitive_post_parameters()
@csrf_protect
@never_cache
# 視圖函數(shù)要渲染的模板位置(registration/login.html)
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
extra_context=None, redirect_authenticated_user=False):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, ''))
if redirect_authenticated_user and request.user.is_authenticated:
redirect_to = _get_login_redirect_url(request, redirect_to)
if redirect_to == request.path:
raise ValueError(
"Redirection loop for authenticated user detected. Check that "
"your LOGIN_REDIRECT_URL doesn't point to a login page."
)
return HttpResponseRedirect(redirect_to)
elif request.method == "POST":
form = authentication_form(request, data=request.POST)
if form.is_valid():
auth_login(request, form.get_user())
return HttpResponseRedirect(_get_login_redirect_url(request, redirect_to))
else:
form = authentication_form(request)
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context)
三 實(shí)戰(zhàn)一
1 編輯mysite/account/urls.py
from django.conf.urls import url from . import views from django.contrib.auth import views as auth_views urlpatterns = [ # 自定義登錄 # url(r'^login/$', views.user_login, name='user_login'), # django內(nèi)置的登錄 url(r"^login/$", auth_views.login, name="user_login"), ]
2 因?yàn)槟J(rèn)的模板位置為registration/login.html,因此我們創(chuàng)建該文檔如下:
{% extends "base.html" %}
{% block title %}登錄{% endblock %}
{% block content %}
<div class="row text-center vertical-middle-sm">
<h1>登錄</h1>
<p>請(qǐng)輸入用戶名和密碼</p>
<!--用具體的URL指明了數(shù)據(jù)的POST目標(biāo)-->
<form class="form-horizontal" action="{% url 'account:user_login' %}" method="post">
{% csrf_token %}
<!--每個(gè)表單元素在一對(duì)P標(biāo)簽內(nèi)-->
<!--{{ form.as_p }}-->
<!--使用Bootstrap樣式使得表單更美麗-->
<div class="form-group">
<label for="{{ form.username.id_for_label }}" class="col-md-5 control-label" style="color:red"><span class="glyphicon glyphicon-user"></span>Username</label>
<div class="col-md-6 text-left">{{ form.username }}</div>
</div>
<div class="form-group">
<label for="{{ form.password.id_for_label }}" class="col-md-5 control-label" style="color:blue"><span class="glyphicon glyphicon-floppy-open"></span>Password</label>
<div class="col-md-6 text-left">{{ form.password }}</div>
</div>
<input type="submit" value="Login">
</form>
</div>
{% endblock %}
3 修改mysite/mysite/settings.py
# 登錄后重定向到http://localhost:8000/blog/頁面 LOGIN_REDIRECT_URL = '/blog/'
4 測(cè)試


四 實(shí)戰(zhàn)二
1 編輯mysite/account/urls.py
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
# 自定義登錄
# url(r'^login/$', views.user_login, name='user_login'),
# django內(nèi)置的登錄
url(r"^login/$", auth_views.login, name="user_login"),
url(r"^new-login/$", auth_views.login, {"template_name": "account/login.html"}),
]
2 測(cè)試


希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
- python,Django實(shí)現(xiàn)的淘寶客登錄功能示例
- 詳解Django框架中用戶的登錄和退出的實(shí)現(xiàn)
- 淺談django中的認(rèn)證與登錄
- django的登錄注冊(cè)系統(tǒng)的示例代碼
- 在Django中限制已登錄用戶的訪問的方法
- Django自定義插件實(shí)現(xiàn)網(wǎng)站登錄驗(yàn)證碼功能
- django用戶注冊(cè)、登錄、注銷和用戶擴(kuò)展的示例
- Python中Django框架利用url來控制登錄的方法
- Django實(shí)戰(zhàn)之用戶認(rèn)證(用戶登錄與注銷)
- Django中使用第三方登錄的示例代碼
- django用戶登錄和注銷的實(shí)現(xiàn)方法
- Django框架實(shí)現(xiàn)的普通登錄案例【使用POST方法】
相關(guān)文章
python編程使用PyQt制作預(yù)覽窗口游戲中的小地圖
這篇文章主要為大家介紹了python使用PyQt制作預(yù)覽窗口游戲中的小地圖實(shí)現(xiàn)示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
對(duì)python條件表達(dá)式的四種實(shí)現(xiàn)方法小結(jié)
今天小編就為大家分享一篇對(duì)python條件表達(dá)式的四種實(shí)現(xiàn)方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python3+pycuda實(shí)現(xiàn)執(zhí)行簡單GPU計(jì)算任務(wù)
GPU的加速技術(shù)在深度學(xué)習(xí)、量子計(jì)算領(lǐng)域都已經(jīng)被廣泛的應(yīng)用。這篇文章就來和大家聊聊Python3如何利用pycuda執(zhí)行簡單GPU計(jì)算任務(wù)?,感興趣的可以了解一下2023-03-03
python使用psutil模塊獲取系統(tǒng)狀態(tài)
作為程序猿,大家可能都熟悉linux系統(tǒng)的基礎(chǔ)信息獲取方法都是通過shell來獲取,但是在python中,我們還可以使用psutil模塊來獲取系統(tǒng)信息。psutil模塊把shell查看系統(tǒng)基礎(chǔ)信息的功能都包裝了下,使用更加簡單,功能豐富。2016-08-08
python和pygame實(shí)現(xiàn)簡單俄羅斯方塊游戲
這篇文章主要為大家詳細(xì)介紹了python和pygame實(shí)現(xiàn)簡單俄羅斯方塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
python list.sort()根據(jù)多個(gè)關(guān)鍵字排序的方法實(shí)現(xiàn)
Python list內(nèi)置sort()方法用來排序,也可以用python內(nèi)置的全局sorted()方法來對(duì)可迭代的序列排序生成新的序列,本文詳細(xì)的介紹了python list.sort()根據(jù)多個(gè)關(guān)鍵字排序,感興趣的可以了解一下2021-12-12
Python+matplotlib實(shí)現(xiàn)折線圖的美化
這篇文章主要和大家分享一個(gè)非常有趣的Python教程—如何美化一個(gè)?matplotlib折線圖。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05

