国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-01-04 16:15:56
字體:
來源:轉載
供稿:網(wǎng)友

前言

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

當然, 在使用前, 記得在工程目錄的settings.py中設置好LOGIN_URL

使用方法

1. URLconf中裝飾

from django.contrib.auth.decorators import login_required, permission_requiredfrom django.views.generic import TemplateViewfrom .views import VoteViewurlpatterns = [ 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_requiredfrom django.http import HttpResponse@login_requireddef my_view(request): if request.method == 'GET':  # <view logic>  return HttpResponse('result')

3. 裝飾類的視圖

from django.contrib.auth.decorators import login_requiredfrom django.utils.decorators import method_decoratorfrom django.views.generic import TemplateViewclass 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_requiredfrom django.http import HttpResponseRedirectfrom django.shortcuts import renderfrom django.views.generic import Viewfrom .forms import MyFormclass 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)才可以訪問某些頁面,如果用戶沒有登陸而直接訪問就會跳轉到登陸界面。

要實現(xiàn)這樣的需求其實很簡單:

      1、在相應的 view 方法的前面添加 django 自帶的裝飾器 @login_required

      2、在 settings.py 中配置 LOGIN_URL 參數(shù)

      3、修改 login.html 表單中的 action 參數(shù)

# views.pyfrom djanco.contrib.auth.decorators import login_requiredfrom django.shortcuts import render_to_response@login_requireddef index(request):return render_to_response('index.html')
# settings.py....LOGIN_URL = '/accounts/login/' # 根據(jù)你網(wǎng)站的實際登陸地址來設置....

如果要使用 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 -->

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。   


注:相關教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 周至县| 庄浪县| 祥云县| 韩城市| 琼结县| 顺昌县| 正镶白旗| 吴忠市| 平顶山市| 资阳市| 闵行区| 元江| 丹东市| 慈利县| 察隅县| 长春市| 陈巴尔虎旗| 若尔盖县| 启东市| 鹰潭市| 开封市| 永年县| 买车| 沾化县| 中西区| 上犹县| 永兴县| 尼玛县| 闽清县| 洛川县| 维西| 阿巴嘎旗| 彭泽县| 威海市| 弋阳县| 错那县| 合肥市| 永平县| 财经| 资溪县| 凤凰县|