0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UpdateView による 他のユーザーが変更できないようにする!

Posted at

#他のユーザーは、投稿を編集できないようにする。
deletのclassもget_querysetでアクセス不可にする。

views.py
class Edit(LoginRequiredMixin, generic.UpdateView):
    model = Content
    template_name = 'content/edit.html'
    form_class = ContentEdit
    success_url ='/content/index/'

    # ログインしているユーザが作ったもでなければアクセス不可
    def get_queryset(self):
        # このビューで生成されるベースとなるクエリセットを取得
        base_qs = super(Edit, self).get_queryset()
        # さらにユーザIDで絞った結果を返す。(存在しないので404が返る)
        # 条件分岐してエラーページを出しても可
        return base_qs.filter(owner=self.request.user)

    def form_valid(self, form):
        messages.success(self.request, '編集できました!')
        return super().form_valid(form)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?