【发布时间】:2019-12-16 21:28:03
【问题描述】:
我有这个 UpDateView 类,我只需要文章的作者就可以编辑博客。我有 CreateView 类的解决方案(使用 def Form_valid),但它不适用于 UpdateView 类 :::
class ArticleUpdateView(LoginRequiredMixin,UpdateView):
model = models.Article
template_name = 'article_edit.html'
fields = ['title','body']
login_url = 'login'
class ArticleCreateView(LoginRequiredMixin,CreateView):
model = models.Article
template_name = 'article_new.html'
fields = ['title','body',]
login_url='login'
def form_valid(self,form):
form.instance.author = self.request.user
return super().form_valid(form)
【问题讨论】:
-
为什么不呢?将 form_valid 添加到更新类时会发生什么?
-
实际上它让我感到惊讶,因为从逻辑上它应该可以工作。我创建了 3 个用户,但每个用户都可以编辑和删除其他用户的帖子。我为其他视图复制并粘贴了 Form_valid,但它只适用于 CreateView....
标签: django django-views