I've made it so that any user that signs up with a username will automatically have their username converted to lowercase. user/forms.py
I now have the issue where I can't figure a way to convert the username in the login form to become lower when submitted. As I would still like the user to be able to enter their name in the case of their choice.
I've tried the python tolower() function on a few things in the LoginView, yet unsuccessful. If anyone has an idea on how I could achieve this please let me know.
Here is the user/forms.py
class UserSignupForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
def clean_username(self):
username = self.cleaned_data["username"].lower()
if not re.match(r"^[A-Za-z0-9_]+$", username):
raise forms.ValidationError(
"Sorry , you can only have alphanumeric, _ or - in username"
)
else:
return username
Here is the user/urls.py
path("signup/", user_view.signup, name="signup"),
path("login/",auth_view.LoginView.as_view(template_name="user/login.html"),name="login"),
You can subclass the AuthenticationForm:
# app_name/forms.py
from django.contrib.auth.forms import AuthenticationForm
class MyAuthenticationForm(AuthenticationForm):
def clean_username(self):
return self.cleaned_data['username'].lower()
then you inject that form in the LoginView:
from app_name.forms import MyAuthenticationForm
# …
path('login/',
auth_view.LoginView.as_view(
form_class=MyAuthenticationForm,
template_name='user/login.html'
),
name='login'
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With