最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Django中l(wèi)ogin_required裝飾器的深入介紹

 更新時間:2017年11月24日 09:12:41   作者:James  
這篇文章主要給大家介紹了關(guān)于Django中l(wèi)ogin_required裝飾器的使用方法,并給大家進行了實例借鑒,利用@login_required實現(xiàn)Django用戶登陸訪問限制,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

Django提供了多種裝飾器, 其中l(wèi)ogin_required可能是經(jīng)常會使用到的。 這里介紹下四種使用此裝飾器的辦法。

當(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. 裝飾通過Mixin類繼承來實現(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ā)過程中,經(jīng)常會遇到這樣的需求:用戶登陸系統(tǒng)才可以訪問某些頁面,如果用戶沒有登陸而直接訪問就會跳轉(zhuǎn)到登陸界面。

要實現(xià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è)置
....

如果要使用 django 默認登陸地址,則可以通過在 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é)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。   

相關(guān)文章

  • Python列表(List)知識點總結(jié)

    Python列表(List)知識點總結(jié)

    在本篇文章中小編給大家分享了關(guān)于Python列表(List)知識點一直對應(yīng)的實例內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • Python?numpy生成矩陣基礎(chǔ)用法實例代碼

    Python?numpy生成矩陣基礎(chǔ)用法實例代碼

    矩陣是matrix類型的對象,該類繼承自numpy.ndarray,任何針對ndarray的操作,對矩陣對象同樣有效,下面這篇文章主要給大家介紹了關(guān)于Python?numpy生成矩陣基礎(chǔ)的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié)

    python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié)

    下面小編就為大家分享一篇python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 解決python中使用PYQT時中文亂碼問題

    解決python中使用PYQT時中文亂碼問題

    今天小編就為大家分享一篇解決python中使用PYQT時中文亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python之parser.add_argument解讀

    Python之parser.add_argument解讀

    這篇文章主要介紹了Python之parser.add_argument解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python實現(xiàn)疫情地圖可視化

    Python實現(xiàn)疫情地圖可視化

    這篇文章主要介紹了Python如何實現(xiàn)疫情地圖可視化,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-02-02
  • 利用Python實現(xiàn)讀取Word表格計算匯總并寫入Excel

    利用Python實現(xiàn)讀取Word表格計算匯總并寫入Excel

    這篇文章主要給大家介紹了關(guān)于如何利用Python實現(xiàn)讀取Word表格計算匯總并寫入Excel的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • python中日期和時間格式化輸出的方法小結(jié)

    python中日期和時間格式化輸出的方法小結(jié)

    這篇文章主要介紹了python中日期和時間格式化輸出的方法,實例總結(jié)了Python常見的日期與事件操作技巧,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • python實現(xiàn)網(wǎng)上購物系統(tǒng)

    python實現(xiàn)網(wǎng)上購物系統(tǒng)

    這篇文章主要為大家詳細介紹了python實現(xiàn)網(wǎng)上購物系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Python socket實現(xiàn)簡單聊天室

    Python socket實現(xiàn)簡單聊天室

    這篇文章主要為大家詳細介紹了Python socket實現(xiàn)簡單聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評論

松阳县| 肥乡县| 灯塔市| 根河市| 梁河县| 电白县| 乐都县| 榆树市| 鄂温| 永春县| 云安县| 文安县| 宝清县| 浠水县| 铜梁县| 武冈市| 清镇市| 临颍县| 长汀县| 饶阳县| 镇巴县| 理塘县| 桂东县| 泽库县| 莱阳市| 建宁县| 墨江| 云龙县| 沂水县| 休宁县| 拉萨市| 灵川县| 杂多县| 太和县| 彰武县| 德江县| 桓仁| 黑龙江省| 大名县| 昔阳县| 德州市|