LoginSignup
2
1

More than 1 year has passed since last update.

【Django】submitボタンを複数用意してpost処理を条件によって使い分ける

Posted at

問題

よく何かの登録画面の「送信」や「登録」などで使うsubmitボタンだが、「承認」と「非承認」のように複数のsubmitボタンを用意して処理をかき分ける時はどうすればいいのか。

結論

nameタグで区別するだけ。

実装

typeタグに加えてnameタグを一意に設定

テンプレートファイル
<form method="post">
    {% csrf_token %}
    <button type="submit" name="reject">非承認</button>
    <button type="submit" name="approve">承認</button>
</form>

nameタグ別に処理を記入

views.py
def post(self, request, id):
    if "approve" in request.POST:
        # 承認時の処理
    elif "reject" in request.POST:
        # 非承認時の処理
2
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
2
1