我已经编写了一个视图来发送表单,但是我必须对用户进行分级,所以我编写了两个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)
1条答案
按热度按时间2lpgd9681#
但现在我的作者用户无法发送文章。
像
user
一样也应该由author
编写的文章,您还应该在FieldsMixin
中的self.fields
中包含author
字段,以便: