我是django框架的新手,我正在从头开始创建一个博客应用程序,我正在集成django-taggit来标记这些文章。我想要完成的是,我只希望某些用户能够添加新标签,其余的只能使用现有标签。就像stackoverflow实现的那样,它允许具有一定声誉的用户添加新标签。
我如何实现这一目标?
答案 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):
...
或者,您也可以在模板中实现控件。