LoginSignup
1
0

More than 5 years have passed since last update.

Django2 公式チュートリアルのPollsアプリの機能追加【pollsの削除】

Last updated at Posted at 2018-04-16

前提

views.pyに削除機能を追加

polls/views.py
class DeleteView(generic.DeleteView):
    model = Question
    form_class = QuestionForm

    success_url = reverse_lazy('polls:index')

    def delete(self, request, *args, **kwargs):
        result = super().delete(request, *args, **kwargs)
        messages.success(
            self.request, '「{}」を削除しました'.format(self.object))
        return result

urls.pyに削除のURLを追加

polls/urls.py
    path('<int:pk>/delete/',views.DeleteView.as_view(), name='delete'),

question_confirm_delete.htmlを追加

templates/polls/question_confirm_delete.html
<p>{{ object }} を削除していいですか?</p>
<form method="post">
  <button>削除する</button>
  {% csrf_token %}
</form>

detail.htmlに削除ボタンを追加

templates/polls/detail.html
<a href="{% url 'polls:delete' question.id %}">削除</a>

これで動くと思います。
動かない場合は、当記事冒頭の前提条件を確認してください。

1
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
1
0