django 为什么我写了两个mixin后我的文章没有发送?

jw5wzhpr  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(104)

我已经编写了一个视图来发送表单,但是我必须对用户进行分级,所以我编写了两个mixin,但是现在我的author用户无法发送文章。
查看:

from .mixins import FieldsMixin, FromValidMixin
class ArticleCreate(LoginRequiredMixin, FromValidMixin , FieldsMixin , CreateView):
    model = Article
    template_name = "registration/article-create-update.html"

混合物:

from django.http import Http404

class FieldsMixin():
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_superuser:
            self.fields = ["author" ,"title" , "slug" , "category" , "description" , "thumbnail" , "publish" , "status"]
        
        elif request.user.is_author:
            self.fields = ["title" , "slug" , "category" , "description" , "thumbnail" , "publish" , "status"]

        else:
            raise Http404("You can't see this page")

        return super().dispatch(request, *args, **kwargs)

class FromValidMixin():
    def form_valid(self, form):
        if self.request.user.is_superuser:
            form.save()
        else:
            self.obj = form.save(commit=False)
            self.obj.author = self.request.user
            self.obj.status = 'd'
        return super().form_valid(form)
2lpgd968

2lpgd9681#

但现在我的作者用户无法发送文章。
user一样也应该由author编写的文章,您还应该在FieldsMixin中的self.fields中包含author字段,以便:

class FieldsMixin():
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_superuser or request.user.is_author:
            self.fields = ["author", "title", "slug", "category", "description", "thumbnail", "publish", "status"]
        else:
            raise Http404("You can't see this page")

        return super().dispatch(request, *args, **kwargs)

相关问题