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 5 years have passed since last update.

Django2.0でブログの作成(その8:記事の削除)

Posted at

その8

記事の削除です。
一つ前の記事の編集と全く同じです、むしろそれより簡単。
いきますよー

views.py

blog/views.py
def delete_article(request, pk):
    article = Post.objects.get(id=pk)

    if request.user.id == article.author.id: #記事の投稿者だけ削除実行できる。
        article.delete()
    return redirect('blog:post_list')

urls.py

blog/urls.py
path('article/<int:pk>/delete', views.delete_article, name='delete_article'),

追加

edit_article.html

ここでは、編集画面に消去ボタンをつけます。
そもそも編集画面は記事の投稿者しか入れないページなので(その7でやりました!)
html上でif文を書く必要はありませんね。

edit_article.html
<a href="{% url 'blog:delete_article' form.instance.id %}" class="btn btn-danger">消去</a>

この一文を<form></form>内に追加します。

確認

スクリーンショット 2018-10-09 17.12.48.png
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?