简体   繁体   中英

How to add custom title for form fields in Django?

I created this simple form:

class FormsProject(forms.Form):
    fullname = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control mb-3', 'placeholder': 'Basir Payenda'}))
    Django = forms.BooleanField(widget=forms.CheckboxInput())
    Flask = forms.BooleanField(widget=forms.CheckboxInput())
    gender = forms.ChoiceField(
        widget=forms.RadioSelect(), choices=SELECT_GENDER)
    comments = forms.CharField(widget=forms.Textarea(attrs={
        'class': 'form-control mb-2',
        'rows': 4,

    }), help_text='Write here your message!')

and it looks like

在此处输入图像描述

How do I add a title asking something like "Your favorite frameworks: " before those checkbox, let me illustrate it:

在此处输入图像描述

Help me add that title before checkbox, thanks

You can rendering fields manually , for example:

<div class="form-group">
  <div class="input-group">
    <label>{{ form.fullname.label }}</label>
    {{ form.fullname }}
    {{ form.fullname.errors }}
  </div>
</div>

<div class="form-group">
  <h2>Your favorite frameworks</h2>
</div>

<div class="form-group">
  <div class="input-group">
    <label>{{ form.Django.label }}</label>
    {{ form.Django }}
    {{ form.Django.errors }}
  </div>
</div>

<div class="form-group">
  <div class="input-group">
    <label>{{ form.Flask.label }}</label>
    {{ form.Flask }}
    {{ form.Flask.errors }}
  </div>
</div>

See this docs for more https://docs.djangoproject.com/en/dev/topics/forms/#rendering-fields-manually

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM