限制某些用户添加新标签,django-taggit?

时间:2014-07-04 04:34:29

标签: python django django-taggit

我是django框架的新手,我正在从头开始创建一个博客应用程序,我正在集成django-taggit来标记这些文章。我想要完成的是,我只希望某些用户能够添加新标签,其余的只能使用现有标签。就像stackoverflow实现的那样,它允许具有一定声誉的用户添加新标签。

我如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

你可以做几件事。首先,我肯定会实现某种UserProfile模型,即具有reputation属性,然后你有很多选项可以完成你的任务,即

使用@user_passes_test装饰器,您可以在其中创建自己的函数以传递给装饰器。

def at_least_fifty_rep(user): 
    my_profile = ... # get the user profile
    return my_profile.reputation > 50

@user_passes_test(at_least_fifty_rep)
def my_custom_view(request):
    ...

或者,您也可以在模板中实现控件。

相关问题