Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...