Django - limiting url access to superusers

拥有回忆 提交于 2019-12-11 06:26:36

问题


In my urlconf, i have:

url(r'^sssssh/(.*)', staff_only_app.site.root),

What I'd like to do is limiting any access to this application to superusers. I tried this:

url(r'^sssssh/(.*)', user_passes_test(staff_only_app.site.root, lambda u: u.is_superuser)),

But it complains that decorate takes exactly 1 argument, and I gave two.

I'm thinking about currying the decorator via functools.partial, but thought I may be missing some more obvious solution.


回答1:


Very late reply!...

I think it's just a quick and dirty syntax hangup:

url(r'^sssssh/(.*)', user_passes_test(lambda u: u.is_superuser)(staff_only_app.site.root),

^I think this is the strange but correct syntax for passing an argument to a decorator.

But on 2nd thought, you can only decorate view functions, not entire sites.




回答2:


Write a decorator similar to Django's login_required or f.ex. this one http://djangosnippets.org/snippets/254/ and decorate the view.




回答3:


Use the user_passes_test decorator.

example:

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_superuser)

def sample_view(request):


来源:https://stackoverflow.com/questions/3139284/django-limiting-url-access-to-superusers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!