Django Adding Placeholders To Django Built In Login Forms
Solution 1:
save this content in forms.py
from django import forms
from django.contrib.auth.formsimportAuthenticationFormfrom django.forms.widgetsimportPasswordInput, TextInputclassCustomAuthForm(AuthenticationForm):
username = forms.CharField(widget=TextInput(attrs={'class':'validate','placeholder': 'Email'}))
password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))
in your main urls.py
(where your login view called)
from django.contrib.auth import views as auth_views
from app.forms import CustomAuthForm
urlpatterns = [
url(r'^login/$', auth_views.login, name='login', kwargs={"authentication_form":CustomAuthForm}),
]
the extra thing that we done here is added an kwargs kwargs={"authentication_form":CustomAuthForm}
please use this for your future reference django.contrib.auth.views.LoginView and django.contrib.auth.forms.AuthenticationForm
Solution 2:
save this content in forms.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput
classMyAuthForm(AuthenticationForm):
classMeta:
model = User
fields = ['username','password']
def__init__(self, *args, **kwargs):
super(MyAuthForm, self).__init__(*args, **kwargs)
self.fields['username'].widget = forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Username'})
self.fields['username'].label = False
self.fields['password'].widget = forms.PasswordInput(attrs={'class': 'form-control', 'placeholder':'Password'})
self.fields['password'].label = False
and save this content in main urls.py
from users.forms importMyAuthFormurlpatterns= [
...
path('', auth_views.LoginView.as_view(template_name='users/login.html', authentication_form=MyAuthForm), name='login'),
...
]
please refer this site: https://github.com/django/django/blob/master/django/contrib/auth/views.py
Solution 3:
Yes you can add placeholder something like this,
classLoginForm(forms.ModelForm):
classMeta:
model = YourModelNamewidgets= {
'username' : forms.TextInput(attrs = {'placeholder': 'Username'}),
'password' : forms.PasswordInput(attrs = {'placeholder': 'Password'}),
}
I hope this will work for you.
Solution 4:
In response to your comment on the other answer. You can subclass the the Authentication form.
from django import forms
from django.contrib.auth.formsimportAuthenticationFormfrom django.forms.widgetsimportPasswordInput, TextInputclassPlaceholderAuthForm(AuthenticationForm):
username = forms.CharField(widget=TextInput(attrs={'placeholder': 'Email'}))
password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))
or see the other function that was put in this question. --> How can I override the django AuthenticationForm input css class?
Solution 5:
You can add another filter
@register.filter(name="add_place_holder")defadd_place_holder(field, place_holder=None):
if place_holder == None:
return field
else:
field.field.widget.attrs.update({'placeholder': place_holder})
return field
Post a Comment for "Django Adding Placeholders To Django Built In Login Forms"