2
1

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

Django template formで渡されるパラメータを項目ごとに手動で装飾したいメモ

Last updated at Posted at 2020-11-19

Python 3.8.5
Django version 3.1.2

備忘録

/formtest/formman$ tree
.
├── admin.py
├── apps.py
├── forms.py ★つくりました。
├── __init__.py
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_auto_20201119_1155.py
│   └── __init__.py
├── models.py ★書きました
├── templates ★作りました
│   ├── base.html
│   └── formman
│       └── form.html
├── tests.py
└── views.py ★書きました

3 directories, 12 files

Djangoのチュートリアルやリファレンスを読んでると
以下の用に記載するとDjangoのViewはTemplateにformの項目を表示してくれると書いてあります。

            {{ form.as_p }}

こんな感じで書くんですよね。
image.png

実際にrunserverして、HTMLソースをみるとこんな感じ。

image.png
ということは、form.as_pに該当するのは以下の部分。

<p><label for="id_id">ID:</label> <input type="number" name="id" required id="id_id"></p>
<p><label for="id_name">名:</label> <input type="text" name="name" maxlength="100" required id="id_name"></p>
<p><label for="id_number">値:</label> <input type="number" name="number" required id="id_number"></p>

form.as_pを以下のように書き換えると…。

            {{ form.id.label }}
            {{ form.name.label }}
            {{ form.number.label }}

            {{ form.id }}
            {{ form.name }}
            {{ form.number }}

おお。うまく行った。
image.png

これで項目ごとに装飾できそうです。

2
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?