超级用户是否可以创建多个帖子,而用户只能发布一个帖子?

时间:2016-01-15 00:50:29

标签: django

目前,用户只能创建一个帖子。但作为超级用户,我想创建多个。我试着在管理页面中这样做,但它不起作用。有没有办法做到这一点?

@login_required
def add_category(request):
    if Category.objects.filter(author=request.user).exists():
        return render(request,'main/category_already_exists.html')
    if request.method == 'POST':
        category = Category(author=request.user)
        form = CategoryForm(request.POST, instance=category)
        if form.is_valid():
            form.save(commit=True)
            return redirect('index')

    else:
        form = CategoryForm()

    return render(request, 'main/add_category.html', {'form':form})

1 个答案:

答案 0 :(得分:2)

@login_required
def add_category(request):
    if not request.user.is_superuser and Category.objects.filter(author=request.user).exists():
         return render(request,'main/category_already_exists.html')
    if request.method == 'POST':
        category = Category(author=request.user)
        form = CategoryForm(request.POST, instance=category)
        if form.is_valid():
            form.save(commit=True)
        return redirect('index')

else:
    form = CategoryForm()

return render(request, 'main/add_category.html', {'form':form})