1

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

我如何做到这一点?

4

1 回答 1

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):
    ...

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

于 2014-07-04T05:00:30.967 回答