<label>Name: <input name="name" placeholder="John Doe" type="text" /></label>
This would appear in the form as:
To add placeholders to Django forms, simply specify placeholder as an attribute to the form widget.
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'John Doe'})
This is actually a pretty general and useful way of passing attributes to Django forms. We can pass other attributes like 'autofocus':'autofocus'. Autofocus is when we want the form field to be in focus automatically when the page loads.
This is for forms created in our app's own forms.py. If we want to pass attributes to forms created by a Django package, we have to pass those attributes at the form's init.
Say, to add attributes to a form called RegistrationForm from some Django package with fields username and password, first create a form in project's forms.py that inherits RegistrationForm. In the __init__ function of the inherited form, pass all the attributes to the form widgets.
class MyRegistrationForm(RegistrationForm):
def __init__(self, *args, **kwargs):
super(MyRegistrationForm, self).__init__(*args, **kwargs)
self.fields['username'].widget.attrs['placeholder'] = u'Username'
self.fields['password'].widget.attrs['placeholder'] = u'Password'
def __init__(self, *args, **kwargs):
super(MyRegistrationForm, self).__init__(*args, **kwargs)
self.fields['username'].widget.attrs['placeholder'] = u'Username'
self.fields['password'].widget.attrs['placeholder'] = u'Password'
Now while using the form, use it as
form = MyRegistrationForm()
This will display base form with the attributes passed to to it at init.
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog.
ReplyDeleteReally very informative post you shared here. Kindly keep blogging.
If anyone wants to become a Front end developer learn from Javascript Online Training from India .
or learn thru JavaScript Online Training from India.
Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai
I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
ReplyDeleteI know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Thanks for sharing your valuable post and keep doing Looking towards more
ReplyDeleteTo Enhance and Explore the Technical Knowledge's Learn Python
python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery
Thanks you for sharing the unique content. you have done a great job. thank you for sharing such a unique content.keep it up!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
Fon perde modelleri
ReplyDeletesms onay
Turkcell Mobil Ödeme Bozdurma
NFT NASIL ALINIR
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları
ümraniye beko klima servisi
ReplyDeletebeykoz lg klima servisi
pendik vestel klima servisi
pendik arçelik klima servisi
ümraniye mitsubishi klima servisi
üsküdar beko klima servisi
pendik lg klima servisi
pendik alarko carrier klima servisi
ümraniye lg klima servisi