django crispy forms - add field help text? - django

Looking through the crispy forms I cannot find if help text is supported. im trying to add some help text to the select multiple field as per the below
Field('site_types', Title="Site Types", size="15", help_text="Hold down cmd on MacOS or ctrl on windows to select multiple"),
is this supported or would I use some other attribute to achieve this?
Thanks

Here is the working example i used to display help text
class myForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(myForm, self).__init__(*args, **kwargs)
self.fields['site_types'].help_text = "Please select bla bla bla"

Haven't used crispy forms in a little bit but I'm pretty certain you just define help_text like you would on a regular form. Looking at the docs, there are some additional configuration options for the help text if you happen to be using the Bootstrap template pack.

Rather than define help_text in crispy_forms.layout.Field, define it where you define choices (or use the solution by Pavan Kumar T S).
forms.py
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
SITE_TYPES = [
('business', 'Business'),
('education', 'Education'),
('entertainment', 'Entertainment'),
('news', 'News'),
('other', 'Other')
]
class TestForm(forms.Form):
site_types = forms.MultipleChoiceField(
choices=SITE_TYPES,
help_text="Hold down cmd on MacOS or ctrl on windows to select multiple"
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Field('site_types', Title="Site Types", size="15")
)
Form:
Form with help text displayed below the field

Related

Customize crispy form for django-filter

I'm trying to customize form in django using django-crispy-forms and django-filter extensions. Here is my form snippet:
class FilterForm(Form):
def __init__(self, *args, **kwargs):
super(FilterForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'get'
self.helper.layout = Layout(
Row(
Div('country', css_class='col-sm-3'),
Div('brand', css_class='col-sm-3'),
)
)
This is how the layout looks like in browser:
So my questions are: how do I remove p tag with "Filter" text in it and why the arrow button of select control shrinks like this?
To disable paragraph with help text I used this setting
FILTERS_HELP_TEXT_FILTER = False
The style of select box has to do with the bootstrap/firefox issue. Workaround is to restyle select box in css.

Avoid display of help_text in django crispy forms

I am customizing the RegistrationForm from django-registration-redux with django-crispy-forms. For that I have defined a FormHelper which is working fine:
class MyRegistrationForm(RegistrationForm):
def __init__(self, *args, **kwargs):
super(MyRegistrationForm, self).__init__(*args, **kwargs)
helper = self.helper = FormHelper()
# Moving field labels into placeholders
layout = helper.layout = Layout()
for field_name, field in self.fields.items():
layout.append(Field(field_name, placeholder=field.label))
helper.template_pack = 'bootstrap3'
helper.form_show_labels = False
My form is shown as I want: no labels, bootstrap3 and placeholders derived from label.
Now I would also like to suppress the help_text, which is coming from the Field definition. There is a somehow related flag here (help_text_inline), but that is not intended to disable the display of the help text. I can not find a flag to completely disable the display of the help text in the FormHelper documentation. Is this at all possible?
Removing the help text from the Field definition is not really an option, since I am inheriting the RegistrationForm and I do not want to modify it too much.

Django Crispy form define a class for the form field

I am using Django crispy form to design the html for my forms .
I have two radio button which is designed like
Div(InlineRadios('special_question'), css_class="tr-form-r5 "),
I want to define the same class name for both Yes and no options radio buttons .
But not able to figure out how to do that .
I tried like .
InlineRadios('...', css_class="xxx")
But it does not work .
Please suggest me what might I am doing wrong here.
Thanks
You could try to resolve this problem using CSS.
For your form class:
class ConfirmationForm (forms.Form):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
# set form class is well documented
self.helper.form_class = 'form-special'
super(ConfirmationForm, self).__init__(*args, **kwargs)
Then add some a CSS rule:
.form-special input[type="radio"] {
/* whatever you want */
}
You can even find this way more neat and less cumbersome.

Remove Labels in a Django Crispy Forms

Does anybody know if there is a correct way to remove labels in a crispy form?
I got as far as this:
self.fields['field'].label = ""
But it's not a very nice solution.
Just do:
self.helper.form_show_labels = False
To remove all labels.
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>
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
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.
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)
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'
),

Template rendering issue with RadioSelect() widget in Django

I am trying to render a (default) radio button by using the RadioSelect() widget provided in django but whenever I use it the output is blank. But if I let the django forms to load the default renderer than it prints out a dropdown box with the choices just fine. I am pasting here the summary of code that I have written for this.
# In models.py
AUDIO_SETTING_CHOICES = (
('RESTART', 'Play audio from the beginning.'),
('CONTINUE', 'Continue playing previous audio (if same).'),
)
audio_setting = models.CharField(max_length=20, choices=AUDIO_SETTING_CHOICES, default='RESTART')
# In forms.py
class ChapterItemForm(forms.ModelForm):
def __init__(self, user=None, *args, **kwargs):
self.user = user
super(ChapterItemForm, self).__init__(*args, **kwargs)
self.fields['audio_setting'] = forms.ChoiceField(label="How to play the audio?",
widget=forms.RadioSelect())
# In template
<Form>
...
audio setting: {{ form.audio_setting }} <br />
...
So any pointers on what I might be doing wrong?
http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield
you aren't passing any choices to the ChoiceField
self.fields['audio_setting'] = forms.ChoiceField(
choices=AUDIO_SETTING_CHOICES, label="How to play the audio?",
widget=forms.RadioSelect())