Username not validating

I have created a custom user creation form, but somehow have lost the validation of the user name. I can type a single character and it will accept it.

class UpdateUserForm(UserChangeForm):
	class Meta:
		model = User
		fields = ['username', 'email']
		widgets = {
			'username' : forms.TextInput(attrs={'class' : 'form-control'}),
			'email' : forms.TextInput(attrs={'class' : 'form-control'})

The view using the form is in:

def edit_profile(request):
    user = request.user
    user_form = UpdateUserForm(instance=user)
    profile = Profile.objects.get(user=user)
    profile_form = UpdateProfileForm(instance=profile)
    if request.method=="POST":
        user_form = UpdateUserForm(request.POST,instance=user)
        profile_form = UpdateProfileForm(request.POST, instance=profile)
        if user_form.is_valid() and profile_form.is_valid():
            user =
            profile =

            user.first_name = profile.first_name
            user.last_name = profile.last_name          



            username = user.username
            messages.success(request, f'Profile was updated for {username}' )
            return redirect('home')
    context = {'user_form': user_form, 'profile_form': profile_form}
    return render(request, 'profiles/edit_profile.html', context)

A usual I am sure there is something simple i have missed. There is no clash between the forms as they do not have duplicate names.

The HTML is:

{% extends 'profiles/main.html' %}

{% block title %}- Create profile{% endblock %}

{% block content %}

<h1>Please create a profile</h1>

<form method='POST'>
     {% csrf_token %}
        <li>Required. 150 characters or fewer.</li>
        <li>Letters, digits and @/./+/-/_ only.</li>
        <li>Your password can’t be too similar to your other personal information.</li>
        <li>Your password must contain at least 8 characters.</li>
        <li>Your password can’t be a commonly used password.</li>
        <li>Your password can’t be entirely numeric.</li>


     {{ profile_form }}
     <input class='btn btn-primary mt-3' type='submit' name='Update'/>
 {% endblock %}

What “user name” validation are you referring to? I see nothing in what you’ve posted that performs any validation.

I thought - clearly mistakenly - that the UserChangeForm took care of validation of the username.

It can - but you need to identify what or how it needs to be validated. Your code is written to allow it to be anything and to change the username field in the user object to be what’s submitted.

So how would i go about it, I assumed that with UserChangeForm, by the time it gets through ‘is_valid’ it has been checked and is ok. What do i need to add? Am i going about this the wrong way?

Well I guess the first thing we need to clear up is what are you actually looking for this to do?

When you say you want the “user name validated”, what specifically do you mean?

And, what is your objective for this particular form / view?

I am using the form to create a user and associated profile. where the user name adheres to the standard rules:

  • Required. 150 characters or fewer.
  • Letters, digits and @/./+/-/_ only.

Ok, so you are making a couple of mistakes here.

First, in your view you have:

This means you are working with an existing User - the person who is currently logged on and requesting the page generated by this view.

When trying to create a new instance of an object, you would not supply the instance argument to the form. There is no instance for a to-be-created object. You’re also going to need to define form fields for all required model fields.

Regarding the validation of the username field, see:

Thank you. Actually, i posed the code for the ‘update’ not the ‘create’ user, but the issue with validation is the same, I will review those pages and have another try. thanks

I realised I was confusing the validation of the password with the validation of the username, and everything was actually doing what it should. Sorry for wasting your time. I am still learning.