In HTML its easy to give just an attribute to a tag to set autofocus or autocomplete on/off. In Django forms the way around is a little different from the traditional way.
So over-all if you want every field to have some property or value then here is the code how to do it
So after putting this code in your forms.py you would achieve the required functionality. Similarly you can go on adding HTML attributes form here whatever you like to add for example:
read-only, disabled, multiple etc.
Feel free to ask if you have any queries in the comment below.
In HTML you can set a placeholder like
<input name = "name" placeholder = "Awais Ali" type = "text" />So in Django you can do this by
self.fields['Name'].widget.attrs['autofocus'] = 'on'You can also assign auto-complete in a similar fashion
self.fields['Name'].widget.attrs['autofocus'] = 'off'Similarly place-holder can be set like this
self.fields['Name'].widget.attrs['placeholder'] = u'Your Name'This can also be set using dictionary by giving key, value like this
self.fields['Name'].widget.attrs({'placeholder':'Your Name'})
So over-all if you want every field to have some property or value then here is the code how to do it
def __init__(self, *args, **kwargs):
super(LoginForm,self).__init__(*args, **kwargs)
self.fields['username'].widget.attrs['autocomplete'] = 'off'
self.fields['username'].widget.attrs['autofocus'] = 'on'
self.fields['password'].widget.attrs['autocomplete'] = 'off'
username = forms.CharField(label=_("Username"), max_length=30, widget=forms.TextInput())
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput(render_value=False))
So after putting this code in your forms.py you would achieve the required functionality. Similarly you can go on adding HTML attributes form here whatever you like to add for example:
read-only, disabled, multiple etc.
Feel free to ask if you have any queries in the comment below.