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)輸入用戶(hù)名和密碼</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/頁(yè)面 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框架中用戶(hù)的登錄和退出的實(shí)現(xiàn)
- 淺談django中的認(rèn)證與登錄
- django的登錄注冊(cè)系統(tǒng)的示例代碼
- 在Django中限制已登錄用戶(hù)的訪問(wèn)的方法
- Django自定義插件實(shí)現(xiàn)網(wǎng)站登錄驗(yàn)證碼功能
- django用戶(hù)注冊(cè)、登錄、注銷(xiāo)和用戶(hù)擴(kuò)展的示例
- Python中Django框架利用url來(lái)控制登錄的方法
- Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo))
- Django中使用第三方登錄的示例代碼
- django用戶(hù)登錄和注銷(xiāo)的實(shí)現(xiàn)方法
- Django框架實(shí)現(xiàn)的普通登錄案例【使用POST方法】
相關(guān)文章
python實(shí)現(xiàn)ROA算子邊緣檢測(cè)算法
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)ROA算子邊緣檢測(cè)算法,以光學(xué)圖像為例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04淺談Scrapy網(wǎng)絡(luò)爬蟲(chóng)框架的工作原理和數(shù)據(jù)采集
在python爬蟲(chóng)中:requests + selenium 可以解決目前90%的爬蟲(chóng)需求,難道scrapy 是解決剩下的10%的嗎?顯然不是。scrapy框架是為了讓我們的爬蟲(chóng)更強(qiáng)大、更高效。接下來(lái)我們一起學(xué)習(xí)一下它吧。2019-02-02Python實(shí)現(xiàn)刪除時(shí)保留特定文件夾和文件的示例
下面小編就為大家分享一篇Python實(shí)現(xiàn)刪除時(shí)保留特定文件夾和文件的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python的Tornado框架的異步任務(wù)與AsyncHTTPClient
Tornado的奧義就在于異步處理來(lái)提高單線程的Python程序執(zhí)行性能,這里我們就來(lái)詳解Python的Tornado框架的異步任務(wù)與AsyncHTTPClient,需要的朋友可以參考下2016-06-06一文學(xué)會(huì)如何將Python打包后的exe還原成.py
反編譯的第一步就是要將exe文件轉(zhuǎn)換成py文件,下面這篇文章主要給大家介紹了如何通過(guò)一文學(xué)會(huì)將Python打包后的exe還原成.py的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11python使用正則表達(dá)式(Regular Expression)方法超詳細(xì)
這篇文章主要介紹了python使用正則表達(dá)式(Regular Expression)方法超詳細(xì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12pycharm Tab鍵設(shè)置成4個(gè)空格的操作
這篇文章主要介紹了pycharm Tab鍵設(shè)置成4個(gè)空格的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02