Django:“状态”是此函数的无效关键字参数 [英] Django: "state" is an invalid keyword argument for this function
本文介绍了Django:“状态”是此函数的无效关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
个人模型:
class Person(models.Model)
定义应用程序用户的模型包含重要的
信息,如名称,电子邮件,城市/州,年龄等。
user = models.OneToOneField )
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)
查看创建帐户:
def create_account(request):
#函数允许用户创建自己的帐户
如果request.method ==' POST':
#设置一个新用户并给他们一个用户名
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])
#设置加密密码
new_user.set_password(request.POST [password])
new_user.save()
#将新用户添加到数据库
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')
任何想法为什么会收到此错误?
解决方案
用户
模型中没有状态
字段,但是您尝试传递当您创建 new_user
。
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?
解决方案
There's no state
field in User
model but you tried to pass it when you create new_user
.
这篇关于Django:“状态”是此函数的无效关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文