Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
Django adding placeholders to django built in login forms

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

My template:

<div class="form-group">
    <div class="col-md-12">
        {{ form.username|add_class:'form-control' }}
    </div>
</div>
<div class="form-group">
    <div class="col-md-12">
        {{ form.password|add_class:'form-control' }}
    </div>
</div>

How can i do this?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório

0

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 MyAuthForm(AuthenticationForm):
    class Meta:
        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 import MyAuthForm

urlpatterns = [
   ...
   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

about 4 years ago · Santiago Trujillo Relatório

0

Yes you can add placeholder something like this,

class LoginForm(forms.ModelForm):
    class Meta:
        model = YourModelName
        widgets = {
            'username' : forms.TextInput(attrs = {'placeholder': 'Username'}),
            'password' : forms.PasswordInput(attrs = {'placeholder': 'Password'}),
        }

I hope this will work for you.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda