8👍
You could edit the field.html
template:
https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7
Add a FormHelper
attribute to your form that controls the label rendering and use it in that template if
. Custom FormHelper
attributes are not yet officially documented, because I haven’t had time, but I talked about them in a keynote I gave, here are the slides:
https://speakerdeck.com/u/maraujop/p/django-crispy-forms
- [Django]-Do we need to upload virtual env on github too?
- [Django]-How to make two django projects share the same database
- [Django]-Determine variable type within django template
20👍
Works with Boostrap ( see documentation )
In your form :
from crispy_forms.helper import FormHelper
from django import forms
class MyForm(forms.Form):
[...]
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = False
In your template:
<form method='POST' action=''>{% csrf_token %}
{% crispy form %}
<input type='submit' value='Submit' class='btn btn-default'>
</form>
- [Django]-Detect mobile, tablet or Desktop on Django
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
6👍
The solution below lets you remove a label from both a regular or crispy control. Not only does the label text disappear, but the space used by the label is also removed so you don’t end up with a blank label taking up space and messing up your layout.
The code below works in django 2.1.1.
# this class would go in forms.py
class SectionForm(forms.ModelForm):
# add a custom field for calculation if desired
txt01 = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
''' remove any labels here if desired
'''
super(SectionForm, self).__init__(*args, **kwargs)
# remove the label of a non-linked/calculated field (txt01 added at top of form)
self.fields['txt01'].label = ''
# you can also remove labels of built-in model properties
self.fields['name'].label = ''
class Meta:
model = Section
fields = "__all__"
I’m not clear what the problem the OP had with the code snippet he showed, except that he wasn’t putting the line of code in the right place. This seems like the best and simplest solution.
- [Django]-Heroku, postgreSQL, django, comments, tastypie: No operator matches the given name and argument type(s). You might need to add explicit type casts
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
- [Django]-Rendering a value as text instead of field inside a Django Form
4👍
if you are only to remove some labels from input, then explicitly don’t give a label name in model definition, i.e:
field = models.IntegerField("",null=True)
- [Django]-Django: remove a filter condition from a queryset
- [Django]-Constructing Django filter queries dynamically with args and kwargs
- [Django]-Sending HTML email in django
0👍
To Remove All Labels:
self.helper.form_show_labels = False
To Show Specific Lable when all False :
HTML('<span>Your Label</span>')
To Disable Label for Specific field when all is True
self.fields['fieldName'].label = True
Example:
Row(
HTML('<span> Upolad Government ID (Adhar/PAN/Driving Licence)</span>'),
Column('IdProof',css_class='form-group col-md-12 mb-0'),
css_class='form-row'
),
- [Django]-Troubleshooting Site Slowness on a Nginx + Gunicorn + Django Stack
- [Django]-Django get objects not referenced by foreign key
- [Django]-How do you dynamically hide form fields in Django?
0👍
This way you can remove label from field, or change default label text.
class ModelNameForm(forms.ModelForm):
class Meta:
model = OfferResidential
fields = ['field_with_label', 'field_no_label']
# all field mentioned here, with value '' will be shown without label
labels = {
'field_no_label': '',
}
- [Django]-POST jQuery array to Django
- [Django]-Django/DRF – 405 Method not allowed on DELETE operation
- [Django]-CORS error while consuming calling REST API with React