input タグの type 属性を “hidden” に設定することで、非表示の入力フィールド(hiddenフィールド)を作成できる

2024年6月25日

input タグの type 属性を “hidden" に設定することで、非表示の入力フィールド(hiddenフィールド)を作成できます。この hidden フィールドも同様に、ビューにデータを送信する際に利用できます。

例えば:

html
<form method="post" action="{% url 'your_view_name' %}">
    {% csrf_token %}
    <input type="hidden" name="hidden_field" value="hidden_value">
    <input type="text" name="adjust_item_price" placeholder="調整後の価格">
    <button type="submit">価格調整開始</button>
</form>

上記の例では、hidden_field という名前の hidden フィールドが追加されています。この hidden フィールドには値として “hidden_value" が設定されています。フォームを送信すると、hidden フィールドの値もビューに送信され、ビューで request.POST.get('hidden_field') を用いて取得できます。

未分類

Posted by ぼっち