newforms: required field mark

93 views
Skip to first unread message

Oleg Korsak

unread,
Oct 7, 2007, 11:38:25 AM10/7/07
to django...@googlegroups.com
Hello! Is there any built-in method to make my form show required field
label with bold (or some king of mark like "*") and other fields as
normal text

signature.asc

weiss...@yahoo.com

unread,
Oct 18, 2007, 11:21:07 AM10/18/07
to Django users
I'm also looking for this very same solution...

Any advice is greatly appreciated!

Thanks,
Carlos :)

On Oct 7, 11:38 am, Oleg Korsak <kamikaze.is.waiting....@gmail.com>
wrote:


> Hello! Is there any built-in method to make my form show required field
> label with bold (or some king of mark like "*") and other fields as
> normal text
>

> signature.asc
> 1KDownload

Ken

unread,
Oct 18, 2007, 3:47:47 PM10/18/07
to Django users
By default all form fields are required. So, it seems to me that when
you define your forms class, you simply define your fields with a
'label' argument with the '*' tacked on. If you specify that the
field is not required, define it with a label that doesn't have the
'*' tacked on.

Does this work?

Oleg Korsak

unread,
Oct 22, 2007, 2:26:11 PM10/22/07
to django...@googlegroups.com
But I need to mark form fields from the model-generated forms too! I can
put "*" before the label in my own form. but what to do with generated
forms? There is a working example in Django admin part. I'll look to the
code

Ken пишет:

> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com
> To unsubscribe from this group, send email to django-users...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>

signature.asc

weiss...@yahoo.com

unread,
Oct 22, 2007, 2:33:12 PM10/22/07
to Django users
I see what you mean but I dont think it will work because I want the
field elements to look sort of like:

[Prompt:] [Textbox] [a red '*' initially or a error message after
validation] [help text if any]

So, in the template, I was hoping to be able do something like:

<b>{{field.label_tag}}:</b>&nbsp;{{field}}
{% if field.error%}
&nbsp;<font color="red"><b>{{ field.error }}</b></font>
{% else %}
{% if field.required %}&nbsp;<font color="red">*</font>{% endif %}
{% endif %}
{% if field.help_text %}}&nbsp;&nbsp;<font
color="silver">{{ field.help_text }}</font>{% endif %}

Not sure if I have all the code correct here but you get the idea...

Thanks to everyone in the Django community!
Carlos :)

panta

unread,
Oct 23, 2007, 10:16:33 AM10/23/07
to Django users
I've done something similar for auto-generated forms (form_for_model()
and form_for_instance()), by derivating a class from BaseForm and
providing my own _html_output() method, which emits the label with
different attributes depending on its "requiredness" (using the
'attrs' parameter to BoundField.label_tag()).
Then pass your derived class to form_for_model() and
form_for_instance() using the form named parameter.

The code:

class ExtForm(forms.BaseForm):
def _html_output(self, normal_row, error_row, row_ender,
help_text_html, errors_on_separate_row):
"Helper function for outputting HTML. Used by as_table(),
as_ul(), as_p()."
from django.newforms.forms import BoundField
from django.newforms.fields import Field
from django.newforms.util import flatatt, ErrorDict,
ErrorList, ValidationError
from django.utils.html import escape
from django.utils.encoding import StrAndUnicode,
smart_unicode, force_unicode
top_errors = self.non_field_errors() # Errors that should be
displayed above all fields.
output, hidden_fields = [], []
for name, field in self.fields.items():
bf = BoundField(self, field, name)
bf_errors = self.error_class([escape(error) for error in
bf.errors]) # Escape and cache in local variable.
if bf.is_hidden:
if bf_errors:
top_errors.extend(['(Hidden field %s) %s' % (name,
force_unicode(e)) for e in bf_errors])
hidden_fields.append(unicode(bf))
else:
if errors_on_separate_row and bf_errors:
output.append(error_row %
force_unicode(bf_errors))
if bf.label:
label = escape(force_unicode(bf.label))
# Only add the suffix if the label does not end in
punctuation.
if self.label_suffix:
if label[-1] not in ':?.!':
label += self.label_suffix
if field.required:
label = bf.label_tag(label, attrs={'class':
'required'}) or ''
else:
label = bf.label_tag(label, attrs={'class':
'optional'}) or ''
else:
label = ''
if field.help_text:
help_text = help_text_html %
force_unicode(field.help_text)
else:
help_text = u''
output.append(normal_row % {'errors':
force_unicode(bf_errors), 'label': force_unicode(label), 'field':
unicode(bf), 'help_text': help_text})
if top_errors:
output.insert(0, error_row % top_errors)
if hidden_fields: # Insert any hidden fields in the last row.
str_hidden = u''.join(hidden_fields)
if output:
last_row = output[-1]
# Chop off the trailing row_ender (e.g. '</td></tr>')
and insert the hidden fields.
output[-1] = last_row[:-len(row_ender)] + str_hidden +
row_ender
else: # If there aren't any rows in the output, just
append the hidden fields.
output.append(str_hidden)
return u'\n'.join(output)

Hope this helps.
Cheers,
Marco


On Oct 22, 8:33 pm, weissbl...@yahoo.com wrote:
> I see what you mean but I dont think it will work because I want the
> field elements to look sort of like:
>
> [Prompt:] [Textbox] [a red '*' initially or a error message after
> validation] [help text if any]
>
> So, in the template, I was hoping to be able do something like:
>

> <b>{{field.label_tag}}:</b> {{field}}
> {% if field.error%}


> <font color="red"><b>{{ field.error }}</b></font>
> {% else %}

> {% if field.required %} <font color="red">*</font>{% endif %}
> {% endif %}
> {% if field.help_text %}} <font

andresj

unread,
Oct 27, 2007, 6:31:55 PM10/27/07
to Django users

On Oct 22, 11:33 am, weissbl...@yahoo.com wrote:
> So, in the template, I was hoping to be able do something like:
>
> <b>{{field.label_tag}}:</b> {{field}}
> {% if field.error%}

> <font color="red"><b>{{ field.error }}</b></font>
> {% else %}
> {% if field.required%} <font color="red">*</font>{% endif %}
> {% endif %}
> {% if field.help_text %}} <font

> color="silver">{{ field.help_text }}</font>{% endif %}

Almost like that, just subsitite field.required to
field.field.required. field is a BoundField, and field.field is the
Field (e.g. CharField, IntegerField) that is set in the Form.

bjsc...@gmail.com

unread,
Nov 18, 2007, 7:20:54 PM11/18/07
to Django users
i wrote a couple of filters and do this....maybe not as efficient..but
works for me...


{% filter label_asterisk_red %}
{{ posting_form|mark_required }}
{% endfilter %}


import re
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

label_exp = re.compile(r'(?P<precede>\<label.*\>.*)\*')

@register.filter("mark_required")
def truncate(form):
for k,v in form.fields.items():
if v.required:
v.label = v.label or k
v.label += '*'
return form

@register.filter("label_asterisk_red")
def label_asterisk_red(value):
return label_exp.sub('\g<precede><font class="required">*</font>',
value)
Reply all
Reply to author
Forward
0 new messages