Home > Enterprise >  How to disiable BIGGERkeysses into an authentication application builded in django?
How to disiable BIGGERkeysses into an authentication application builded in django?

Time:09-23

I want only to create users with lowercases into an authentication app login page

CodePudding user response:

You can make a customized user model [Django-doc] that adds a validator to check if the username is written in lowercase:

# app_name/models.py

from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

def validate_lower(value):
    if not value.islower():
        raise ValidationError(
            _('The username %(value)s should be written in lowercase'),
            params={'value': value},
        )

class User(AbstractUser):
    username_validators = [
        AbstractUser.username_validator,
        validate_lower
    ]

    username = models.CharField(
        _('username'),
        max_length=150,
        unique=True,
        help_text=_('Required. 150 characters or fewer. Letters, digits and @/./ /-/_ only.'),
        validators=username_validators,
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )

and then set the User model you defined in app_name.User as the user model by setting this in the AUTH_USER_MODEL setting [Django-doc].

  • Related