Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add class, id, placeholder attributes to a field in django model forms

I have a django model like below

models.py

class Product(models.Model):     name = models.CharField(max_length = 300)     description = models.TextField(max_length = 2000)     created = models.DateTimeField(auto_now_add = True)     updated = models.DateTimeField(auto_now = True)      def __unicode__(self):         return self.name 

forms.py

class ProductForm(ModelForm):     class Meta:         model = Product         exclude = ('updated', 'created') 

product_form.py(just an example)

 <form enctype="multipart/form-data" action="{% url 'add_a_product' %}" method="post">          <div id="name">            {{form.name}}          </div>           <div id="description">            {{form.description}}          </div>     </form>  

Actually I want to display/render the html output like below

<input id="common_id_for_inputfields" type="text" placeholder="Name" class="input-calss_name" name="Name">  <input id="common_id_for_inputfields" type="text" placeholder="Description" class="input-calss_name" name="description"> 

So finally how to add attributes(id, placeholder, class)to the model form fields in the above code ?

like image 828
Shiva Krishna Bavandla Avatar asked Oct 21 '13 08:10

Shiva Krishna Bavandla


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!"}))

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'}.

What is placeholder in Django?

In this article, we show how to add a placeholder to a Django form field. A placeholder is text that goes into a form field that can be used to tell the user information. This text disappears when a user clicks in the field and begins typing.

What is form Cleaned_data in Django?

form. cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).


1 Answers

You can do the following:

#forms.py class ProductForm(ModelForm):     class Meta:         model = Product         exclude = ('updated', 'created')      def __init__(self, *args, **kwargs):         super(ProductForm, self).__init__(*args, **kwargs)         self.fields['description'].widget = TextInput(attrs={             'id': 'myCustomId',             'class': 'myCustomClass',             'name': 'myCustomName',             'placeholder': 'myCustomPlaceholder'}) 
like image 62
Alex Parakhnevich Avatar answered Oct 08 '22 16:10

Alex Parakhnevich