TyCharm
TyCharm

Reputation: 425

Django: "state" is an invalid keyword argument for this function

I'm trying to add inputs on my application where a user can put in their "age", "city", and "state". It already worked for "first_name", "last_name", and "email", but when I added these fields I got this error thrown at me.

Person Model:

class Person(models.Model):
    """ The model which defines a user of the application. Contains important
    information like name, email, city/state, age, etc """
    user = models.OneToOneField(User)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=200, null=True)
    state = models.CharField(max_length=200, null=True)
    age = models.CharField(max_length=50, null=True)

View for creating an account:

def create_account(request):
    # function to allow a user to create their own account
    if request.method == 'POST':
        # sets a new user and gives them a username
        new_user = User(username = request.POST["username"],
                    email=request.POST["email"],
                    first_name=request.POST["first_name"],
                    last_name=request.POST["last_name"],
                    age=request.POST["age"],
                    city=request.POST["city"],
                    state=request.POST["state"])
        # sets an encrypted password
        new_user.set_password(request.POST["password"])
        new_user.save()
        # adds the new user to the database
        Person.objects.create(user=new_user,
                          first_name=str(request.POST.get("first_name")),
                          last_name=str(request.POST.get("last_name")),
                          email=str(request.POST.get("email")),
                          age=str(request.POST.get("age")),
                          city=str(request.POST.get("city")),
                          state=str(request.POST.get("state")))
        new_user.is_active = True
        new_user.save()
        return redirect('../')
    else:
        return render(request, 'polls/create_account.html')

Any ideas why I get this error?

Upvotes: 0

Views: 983

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

There's no state field in User model but you tried to pass it when you create new_user.

Upvotes: 5

Related Questions