How to add css class and attribute to username,password

161 views
Skip to first unread message

Tal Bar-Or

unread,
Feb 7, 2021, 8:41:35 PM2/7/21
to Django users

Hello ,

I writing project which the user login with my own customized HTML page (boosstrap)

i can manually add the form.username and form.password , but seems they don't get the attribute from my forms.py , dode is added below , please advise

Thanks


urls.py

from django.urls import path from django.contrib.auth import views as auth_views from .views import * urlpatterns = [     path('login/', auth_views.LoginView.as_view(), name = 'login'),  path('signup/', SignUpView.as_view(), name='signup'), ]


views.py

from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.views import LoginView from django.urls import reverse_lazy from django.views import generic from .forms import * class UsersLoginView(LoginView):     template_name = 'login.html'     success_url = 'blog-home'     success_message = 'Welcome to your profile'     form_class = UserLoginForm

forms.py

from django import forms from django.forms import ModelForm from .models import User class UserLoginForm(forms.ModelForm):     username = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'type':'email', 'name':'Username', 'placeholder':'Username'}))     password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'type': 'password', 'name': 'password', 'placeholder':'Password'}))

Sherif Adigun

unread,
Feb 14, 2021, 6:35:47 PM2/14/21
to Django users
Try changing UserLoginForm(forms.ModelForm) to UserLoginForm(forms.Form)

Abubakar Ibrahim Baiwa

unread,
Feb 14, 2021, 6:44:07 PM2/14/21
to django...@googlegroups.com
You should consider using crispy forms plugin. They easily style your forms.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6ac6a8c7-bc9a-46f3-b8cb-e138a76ec298n%40googlegroups.com.

Omkar Parab

unread,
Feb 14, 2021, 7:41:50 PM2/14/21
to django...@googlegroups.com
Install django-widget-tweaks package.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Omkar Parab

unread,
Feb 14, 2021, 7:46:03 PM2/14/21
to django...@googlegroups.com
crispy-forms will give you bootstrap design. If you want to apply your own css then install "django-widget-tweaks". 

Tal Bar-Or

unread,
Feb 15, 2021, 5:39:18 AM2/15/21
to django...@googlegroups.com
Thank you very much all for the helpful advice
Thanks again 

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/I0QLsyReqZU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJY8mfwdUK8eh%2BZynZ2fX4oH6-34g6PEPa9TqSGCpbvhmGcD0A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages