mercredi 3 février 2021

How to customize UserCreationForm validation in django

I'm currently trying to build user registration form using UserCreationForm but it doesn't suit my needs. First of all, Username field accepts not only alphabet symbols and digits but also @ . + - _. I want to make it accept only latin and cyrillic leterrs and digits but I could not find any information. Also i want to change it's other properties like maximum length. Also after we make these changes will both client and server sides validation process deny banned symbols?

Here is some code I already have in forms.py:

class CreateUserForm(UserCreationForm):
class Meta:
    model = User
    fields = ['username', 'email', 'password1', 'password2']

and views.py:

def register(request):
form = CreateUserForm()

if request.method == 'POST':
    form = CreateUserForm(request.POST)
    if form.is_valid():
        form.save()

context = {'form': form}
return render(request, 'register.html', context)

Also, is this default UserCreationForm relevant way to register user? Or it would be better if I write my own one?




Aucun commentaire:

Enregistrer un commentaire