我想製作一個裝飾器,它可以防止用戶看到其他配置文件,但可以看到自己的配置文件。如果用戶的網址爲http://example.com/user/?id=5,我希望在我的django裝飾器中獲得id = 5的值。我怎樣才能得到它的任何例子?如何在django裝飾器中訪問參數值表單url
def admin_or_ownership_required(owner):
def check_admin_or_owner(user):
# pk = request.GET.get('pk', None)
# if pk is not None and pk == user.id:
# return True
if owner == 'Account':
if user.is_superuser or (user.is_active and (user.role == 'admin' or user.role == 'owner')):
return True
else:
return False
elif owner == 'User':
if user.is_superuser or (user.is_active and (user.role == 'admin' or user.role == 'owner')):
return True
else:
return False
else:
return False
return user_passes_test(check_admin_or_owner)
這是我的看法代碼
class AccountDetailView(DetailView):
template_name = 'api/account_detail.html'
model = Account
@method_decorator(admin_or_ownership_required('Account'))
def dispatch(self, *args, **kwargs):
return super(AccountDetailView, self).dispatch(*args, **kwargs)
如何使用請求參數在admin_or_ownershipl_required裝飾
有幾種方法,請顯示您的查看代碼。 – schneck 2014-11-04 10:17:32
我用編碼編輯了我的問題。我希望代碼能夠完全按照區域進行工作。但我不知道如何使用PK的價值。 – 2014-11-04 10:21:21