BigMonkey89WithaLeg BigMonkey89WithaLeg - 3 months ago 58
Python Question

How to prevent user registration form in Django from returning password field can not be null

I have created a user registration form in Django but every time I test it, the user is not being saved to the database because when the password is entered it raises "this field can not be null. I do not understand why this is happening since the form was working before, and it follows the same procedures as other registration forms. I have been trying to figure out the cause of the issue for days and I am still not understanding how the field can be null and still wont work. However in the terminal I am not getting any error messages, and the login form is working because users are created via the admin. Thanks for the help and here is the code I am using.

the user form



class UserForm(forms.ModelForm):
email = forms.EmailField(label="Email Address")
password = forms.CharField(widget=forms.PasswordInput)

class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email', 'password']

def clean_email(self):
email = self.cleaned_data.get('email')
email_qs = User.objects.filter(email=email)
if email_qs.exists():
raise forms.ValidationError("This email already exists.")
return email

def clean_username(self):
username = self.cleaned_data.get('username')
username_qs = User.objects.filter(username=username)
if username_qs.exists():
raise forms.ValidationError("This username already exists.")
return username

def clean_password(self):
password = self.cleaned_data.get('password')
if len(password) <= 8:
raise forms.ValidationError("Password must be longer than 8 characters")
else:
print("password saved")


My view registration function



def register_view(request):
form = UserForm(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
new_user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, new_user)
return redirect('/appname/account/')
return render(request, "appname/index.html", {"form": form})


My html



% block signup %}
<div clss="container-fluid">
<div class="row">
<div class="col-sm-6" style="background-color:yellow;">
<h1>appname</h1>
<p>x and x</p>
</div>
<div>
<div class="col-sm-6">
<h3>Sign Up</h1>
<form method="POST" action="{% url 'appname:index' %}">
{% csrf_token %}
{{ form }}
<input type="submit" class="btn" value="Sign Up"/>
</form>

<h5>Have an Account? <a href="{% url 'appname:login' %}">Login</a></h5>
</div>
</div>
</div>
</div>
{% endblock %}

Answer

If you clean a django form field (using def clean_myfield) you need to return that field

def clean_password(self):
    password = self.cleaned_data.get('password')
    if len(password) <= 8:
        raise forms.ValidationError("Password must be longer than 8 characters")
    else:
        print("password saved")
    return password  #  <--  add this line
Comments