Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add a 'Star *' after a django ModelForm CharField?

i have some necessary fields in my django ModelForm. How can i add a red star (*) after the required fields ?

like image 660
Kozet Avatar asked Sep 02 '12 10:09

Kozet


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

How do I make a field optional in Django?

By default all fields are required. In order to make a field optional, we have to say so explicitly. If we want to make the pub_time field optional, we add blank=True to the model, which tells Django's field validation that pub_time can be empty.

What is Form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.


2 Answers

I'm going to assume you want this to happen automatically, so here's one of a few different ways:

{% for field in form %}
    <label for="{{ field.auto_id }}">{{ field.label_tag }}
    {% if field.field.required %}
        <span class="required">*</span>
    {% endif %}
    </label>
{% endfor %}

Then you can style the asterisk using CSS.

Or, you can add the asterisk using CSS instead if you want:

<style type="text/css">
    span.required:after { content: '*'; }
</style>
{% for field in form %}
    <label for="{{ field.auto_id }}">
    {% if field.field.required %}
        <span class="required">{{ field.label_tag }}</span>
    {% else %}
        {{ field.label_tag }}
    {% endif %}
    </label>
{% endfor %}

This one is probably a better option if you want to do other things with the required field as well.

However, if you will not be accessing the fields individually (such as using {{ form.as_p }}), then you can add a property to your ModelForm:

class FooForm(forms.ModelForm):
    required_css_class = 'required'

That will define all fields that are required as having the 'required' class (and thus, you can use the CSS code I mentioned above to add an asterisk (or whatever else you want to do with it).

like image 64
JoeLinux Avatar answered Oct 17 '22 09:10

JoeLinux


You can also use jQuery in order to select the label or append to it.

for example if you have this bootstrap form

<form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" required="required">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3">
    </div>
  </div>
</form>

Then if you want to add the star to the required fields.

$('input,textarea,select').filter('[required]').parent().parent().find("label").append("*");

Also you may want to specify a class for the required fields labels so you can make them bold or something

$('input,textarea,select').filter('[required]:visible').parent().parent().find("label").addClass("required_label");
like image 31
Othman Avatar answered Oct 17 '22 08:10

Othman