簡體   English   中英

Django視圖的Python裝飾器:檢查UserProfile中的特定設置

[英]Python decorator for Django views: Check for a particular setting in UserProfile

我想編寫一個裝飾器,以便在我的網站上的所有視圖上使用,以首先檢查登錄用戶的UserProfile是否具有特定設置。 在我的情況下,它是user.get_profile.user_status,該值可以是“過期”或“活動”。 如果user_status =“ expired”,我想將其重定向到計費帳戶更新頁面。 如果他們是活躍的,他們可以通過。

我想成為@must_be_active@paywall_check類的東西。

以前從未寫過裝飾器。 關於如何最好地入門的想法?

首先,請閱讀此http://docs.djangoproject.com/zh-CN/1.2/topics/auth/#limiting-access-to-logged-in-users-that-pass-a-test

如果您不編寫裝飾器,則實際上更簡單。

from django.contrib.auth.decorators import user_passes_test

def must_be_active( user ):
    if .... whatever .... 

def paywall_check( user ):
    if .... whatever .... 

@user_passes_test(must_be_active)
def my_view(request):
    do the work

@user_pass_test(paywall_check)
def another_view(request):
    do the work

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM