Django中l(wèi)ogin_required裝飾器的深入介紹
前言
Django提供了多種裝飾器, 其中l(wèi)ogin_required可能是經(jīng)常會(huì)使用到的。 這里介紹下四種使用此裝飾器的辦法。
當(dāng)然, 在使用前, 記得在工程目錄的settings.py中設(shè)置好LOGIN_URL
使用方法
1. URLconf中裝飾
from django.contrib.auth.decorators import login_required, permission_required from django.views.generic import TemplateView from .views import VoteView urlpatterns = [ url(r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))), url(r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())), ]
2. 裝飾基于函數(shù)的視圖
from django.contrib.auth.decorators import login_required from django.http import HttpResponse @login_required def my_view(request): if request.method == 'GET': # <view logic> return HttpResponse('result')
3. 裝飾類的視圖
from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.views.generic import TemplateView class ProtectedView(TemplateView): template_name = 'secret.html' @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(ProtectedView, self).dispatch(*args, **kwargs)
4. 裝飾通過(guò)Mixin類繼承來(lái)實(shí)現(xiàn)
from django.contrib.auth.decorators import login_required from django.http import HttpResponseRedirect from django.shortcuts import render from django.views.generic import View from .forms import MyForm class LoginRequiredMixin(object): @classmethod def as_view(cls, **initkwargs): view = super(LoginRequiredMixin, cls).as_view(**initkwargs) return login_required(view) class MyFormView(LoginRequiredMixin, View): form_class = MyForm initial = {'key': 'value'} template_name = 'form_template.html' def get(self, request, *args, **kwargs): form = self.form_class(initial=self.initial) return render(request, self.template_name, {'form': form}) def post(self, request, *args, **kwargs): # code here
Django 用戶登陸訪問限制 @login_required
在網(wǎng)站開發(fā)過(guò)程中,經(jīng)常會(huì)遇到這樣的需求:用戶登陸系統(tǒng)才可以訪問某些頁(yè)面,如果用戶沒有登陸而直接訪問就會(huì)跳轉(zhuǎn)到登陸界面。
要實(shí)現(xiàn)這樣的需求其實(shí)很簡(jiǎn)單:
1、在相應(yīng)的 view 方法的前面添加 django 自帶的裝飾器 @login_required
2、在 settings.py 中配置 LOGIN_URL 參數(shù)
3、修改 login.html 表單中的 action 參數(shù)
# views.py from djanco.contrib.auth.decorators import login_required from django.shortcuts import render_to_response @login_required def index(request): return render_to_response('index.html')
# settings.py .... LOGIN_URL = '/accounts/login/' # 根據(jù)你網(wǎng)站的實(shí)際登陸地址來(lái)設(shè)置 ....
如果要使用 django 默認(rèn)登陸地址,則可以通過(guò)在 urls.py 中添加如此配置:
# urls.py .... url(r'^accounts/login/', views.login), ....
# login.html <div class="container"> <form class="form-signin" action="/accounts/login/" method="post"> {% csrf_token %} <!--csrf_token:生成令牌--> <h2 class="form-signin-heading" align="center">登錄系統(tǒng)</h2> <label for="inputUsername" class="sr-only">username</label> <input type="text" name="username" id="inputUsername" class="form-control" placeholder="username" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> 記住密碼 </label> </div> <br /> <button class="btn btn-lg btn-primary btn-block" type="submit">登錄</button> <br /> <span style="color: red;">{{ login_err }}</span> </form> </div> <!-- /container -->
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Python檢測(cè)和防御DOS攻擊的最簡(jiǎn)單方法
這篇文章主要介紹了Python檢測(cè)和防御DOS攻擊,首先講解在CentOS上安裝Python3,理解各個(gè)命令的含義,最后介紹了利用Python實(shí)現(xiàn)DDOS入侵檢測(cè),需要的朋友可以參考下2022-11-11python實(shí)現(xiàn)簡(jiǎn)易淘寶購(gòu)物
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易淘寶購(gòu)物,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問題
這篇文章主要介紹了Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02詳解python中flask_caching庫(kù)的用法
這篇文章主要介紹了詳解python中flask_caching庫(kù)的用法,可以在一定的時(shí)間內(nèi)直接返回結(jié)果而不是每次都需要計(jì)算或者從數(shù)據(jù)庫(kù)中查找。flask_caching插件就是提供這種功能的神器,需要的朋友可以參考下2023-05-05一篇文章帶你了解Python和Java的正則表達(dá)式對(duì)比
正則表達(dá)式有元字符及不同組合來(lái)構(gòu)成,通過(guò)巧妙的構(gòu)造正則表達(dá)式可以匹配任意字符串,并完成復(fù)雜的字符串處理任務(wù),希望本片文章能給你帶來(lái)幫助2021-09-09Python實(shí)現(xiàn)的NN神經(jīng)網(wǎng)絡(luò)算法完整示例
這篇文章主要介紹了Python實(shí)現(xiàn)的NN神經(jīng)網(wǎng)絡(luò)算法,結(jié)合完整實(shí)例形式分析了Python使用numpy、matplotlib及sklearn模塊實(shí)現(xiàn)NN神經(jīng)網(wǎng)絡(luò)相關(guān)算法實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-06-06詳解python如何調(diào)用C/C++底層庫(kù)與互相傳值
Python作為一門腳本解釋語(yǔ)言,本身又很好的結(jié)合C++,所以使用Python開發(fā),在性能要求的地方調(diào)用C/C++底層庫(kù),這簡(jiǎn)直是神器。本文詳細(xì)介紹了Python調(diào)用C/C++底層庫(kù),互相傳值問題,下面一起來(lái)看看。2016-08-08