Django框架使用內(nèi)置方法實現(xiàn)登錄功能詳解
本文實例講述了Django框架使用內(nèi)置方法實現(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)
三 實戰(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 因為默認(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>請輸入用戶名和密碼</p> <!--用具體的URL指明了數(shù)據(jù)的POST目標(biāo)--> <form class="form-horizontal" action="{% url 'account:user_login' %}" method="post"> {% csrf_token %} <!--每個表單元素在一對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 測試
四 實戰(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 測試
希望本文所述對大家基于Django框架的Python程序設(shè)計有所幫助。
- python,Django實現(xiàn)的淘寶客登錄功能示例
- 詳解Django框架中用戶的登錄和退出的實現(xiàn)
- 淺談django中的認(rèn)證與登錄
- django的登錄注冊系統(tǒng)的示例代碼
- 在Django中限制已登錄用戶的訪問的方法
- Django自定義插件實現(xiàn)網(wǎng)站登錄驗證碼功能
- django用戶注冊、登錄、注銷和用戶擴展的示例
- Python中Django框架利用url來控制登錄的方法
- Django實戰(zhàn)之用戶認(rèn)證(用戶登錄與注銷)
- Django中使用第三方登錄的示例代碼
- django用戶登錄和注銷的實現(xiàn)方法
- Django框架實現(xiàn)的普通登錄案例【使用POST方法】
相關(guān)文章
淺談Scrapy網(wǎng)絡(luò)爬蟲框架的工作原理和數(shù)據(jù)采集
在python爬蟲中:requests + selenium 可以解決目前90%的爬蟲需求,難道scrapy 是解決剩下的10%的嗎?顯然不是。scrapy框架是為了讓我們的爬蟲更強大、更高效。接下來我們一起學(xué)習(xí)一下它吧。2019-02-02Python實現(xiàn)刪除時保留特定文件夾和文件的示例
下面小編就為大家分享一篇Python實現(xiàn)刪除時保留特定文件夾和文件的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python的Tornado框架的異步任務(wù)與AsyncHTTPClient
Tornado的奧義就在于異步處理來提高單線程的Python程序執(zhí)行性能,這里我們就來詳解Python的Tornado框架的異步任務(wù)與AsyncHTTPClient,需要的朋友可以參考下2016-06-06一文學(xué)會如何將Python打包后的exe還原成.py
反編譯的第一步就是要將exe文件轉(zhuǎn)換成py文件,下面這篇文章主要給大家介紹了如何通過一文學(xué)會將Python打包后的exe還原成.py的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11python使用正則表達(dá)式(Regular Expression)方法超詳細(xì)
這篇文章主要介紹了python使用正則表達(dá)式(Regular Expression)方法超詳細(xì),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12