前言
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 hereDjango 用戶登陸訪問限制 @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)的支持。
新聞熱點
疑難解答
圖片精選