LoginSignup
1
1

More than 5 years have passed since last update.

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

Posted at

第一弾として、【Pollsの新規登録】を扱いました。
https://qiita.com/Liverty/items/bb761908d288b517dac1

今回は第二回目です。Pollsの更新を扱います。
ちなみに前回もそうなのですが、解説はあまりしていません。
公式サイトやその他のサイトで解説を探していただき、まずは動く状態を作るために本記事を利用していただければ幸いです。

前提

  • Django公式サイトで、pollsアプリを制作して、動いている。
  • 前回の私の記事で、pollsの新規登録機能を実装して、動いている。

views.pyにUpdateViewを追加

polls/views.py
class UpdateView(generic.UpdateView):
    model = Question
    form_class = QuestionForm
    success_url = reverse_lazy('polls:index')

    def form_valid(self, form):
        result = super().form_valid(form)
        messages.success(
            self.request, '「{}」を更新しました'.format(form.instance))
        return result

ulrs.pyにUpdate用のpathを追加

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

detail.htmlに更新用ボタンを追加

details.html
<a href="{% url 'polls:update' question.id %}">質問を修正</a>

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

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