Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we give placeholder for a float field in django forms?

Is is possible to give placeholder for a float field in django forms?

I know that We can easily give placeholder for a char field in django like below,

materials_name = forms.CharField(required=False,max_length=200,label= "Material Name", widget=forms.TextInput(attrs={'placeholder': 'Material name'}))

Like this how can i give placeholder for a float field in django forms?

Thanks in advance

like image 376
Sakeer Avatar asked Nov 14 '13 07:11

Sakeer


People also ask

How do I add a placeholder in Django form field?

To do this in Django currently, you have to do something like: comment = forms. CharField(max_length=200, widget=forms. TextInput({ "placeholder": "Text!"}))

What is placeholder in Django?

Placeholders are an easy way to define sections in an HTML template that will be filled with content from the database when the page is rendered. This content is edited using django CMS's frontend editing mechanism, using Django template tags. fullwidth.

How do I add a class or id attribute to a Django form field?

In order to add a class or id attribute to a form in Django, we have to place a widget=form. TextInput (or widget= form. EmailInput, if email input) within the form field (because it's a text field). Inside of this widget, we then have to put, attrs={'class':'some_class'}.


1 Answers

You can do it in the same way:

materials_float = forms.FloatField(required=False,label= "Material Name", widget=forms.NumberInput(attrs={'placeholder': 'Material float'}))

Or:

materials_decimal = forms.DecimalField(required=False,label= "Material Name", widget=forms.NumberInput(attrs={'placeholder': 'Material decimal'}))

just change Field

UPDATE: As @olofom requested, the widget has been replaced by NumberInput to render to number field in HTML

like image 95
juliocesar Avatar answered Oct 10 '22 23:10

juliocesar