巳年じゃないけど Python やろうぜ(その 14)
エラー表示の話
フォームのバリデーションエラーの表示場所をカスタマイズする方法があっさりとわかったので、次なるお題に移る前にさくっと書いておきます。
{% load static %} <!doctype html> <html> <head> <title>顧客検索</title> <link rel="stylesheet" href="{% static 'customer_search/css/search.css' %}"> </head> <body> <h1>顧客検索</h1> <form method="post" action="."> {% csrf_token %} <table> {% for field in form %} <tr> <th>{{ field.label_tag }}</th> <td> {{ field }} {% if field.errors %} <span class="error"> {% for error in field.errors %} {{ error }} {% endfor %} </span> {% endif %} </td> </tr> {% endfor %} </table> <input type="submit" value="検索"> </form> </body> </html>
.error { color: red; } .error::before { content: 'Error!'; }
フォームの表示を簡略化せずにフィールドごとにやれば良かったんですね。
エラーが無いときは通常どおり。