开发者

Django forms templates: Simple question

I have a form, wich all the fields have a "help_text".. "provide your username", "your name here!", etc.

What I wanted to do, is when the form is submitted and the value wasn't provided, to show an error in that

Example without submitting (in the template):

{{ userForm.username }}
<span class="helptext">{{ userForm.username.help_text }}</span>

If the form is submitted and the value is "empty", then it would show in error_messages the required key, in this case "This field is required", but I want to add an extra class to the span if the error type is required.

So, expample when the user submits the form with an empty value:

{{ userForm.username }}
<span class="helptext{% if userForm.username.errors.required %} required{% endif %}">{{ userForm.username.help开发者_如何转开发_text }}</span>

Or something like that, hope you understand guys, thanks!


This should be doable using:

{% if field.errors %}

and

{% if field.field.required %}

Check this post for some general infos on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜