Django adding placeholders to django built in login forms

后端 未结 5 550
灰色年华
灰色年华 2021-01-06 03:13

I\'m using django built-in login forms and i want to add placeholders to username and password.

My template:

<
5条回答
  •  礼貌的吻别
    2021-01-06 04:01

    save this content in forms.py

    from django import forms
    from django.contrib.auth.forms import AuthenticationForm
    from django.forms.widgets import PasswordInput, TextInput
    
    
    class CustomAuthForm(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

提交回复
热议问题