[Django]-'User' object has no attribute 'is_valid' Django

5👍

You are calling is_valid on the User object, but that’s not a method you’ve defined on the User (or that exists already on AbstractUser). If you take a closer look at your tutorial, they’re actually calling it on the UserForm. Forms have a built-in is_valid method used to make sure all the form data submitted is valid (what valid means can differ based on your use case… maybe you’re making sure everything’s filled in, or that something’s a real email address, or that a date is more than six months in the future).

Your code says:

form1=User(request.POST or None)
if form1.is_valid():
form1.save()

It looks like you meant to define form1 as a form object (otherwise that’s maybe the worst variable name ever). You probably have something called UserForm (or similar) in your forms.py. You’ll want to import that, and instantiate it like:

form1 = UserForm(request.POST, instance=request.user)

https://docs.djangoproject.com/en/2.0/ref/forms/validation/

(side note: When you come back to your code in a year, will you remember what form1 was supposed to mean? Try to name your variable something that explains itself–user_form or something similar)

Leave a comment