尝试保存模型时出现 Django TypeError
Django TypeError when trying to save the model
我正在尝试添加待办事项,但当我尝试保存我的模型时,它说待办事项有一个意外的关键字参数。
TypeError at /
todo() got an unexpected keyword argument 'user'
它抛出以下错误
我的models.py
class todo(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
date= models.DateTimeField(auto_now = True)
is_completed = models.BooleanField(default=False)
def __str__(self):
return self.user.username + self.title
我的观点是这样的
def todo(request):
if request.method == "POST":
title = request.POST.get('title')
description = request.POST.get('description')
todo_list = todo(user=request.user, title=title, description= description)
todo_list.save()
messages.success(request, 'Successfully Created')
return render(request, 'main/todo.html')
请帮帮我
您的模型和视图都命名为 todo
,因此如果您在该视图中调用 todo
,该函数将覆盖模型,从而进行递归调用。
您可以使用不同的名称导入 todo
,例如:
from <i>app_name</i>.models import todo as todo_model
def todo(request):
if request.method == 'POST':
title = request.POST.get('title')
description = request.POST.get('description')
todo_list = <b>todo_model</b>.objects.create(
user=request.user,
title=title,
description= description
)
messages.success(request, 'Successfully Created')
return render(request, 'main/todo.html')
但是根据PEP-8 style guidelines,类写在PerlCase
,不是snake_case
。因此,您可能希望将模型重命名为:
class <b>Todo</b>(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
date= models.DateTimeField(auto_now = True)
is_completed = models.BooleanField(default=False)
def __str__(self):
return self.user.username + self.title
然后您可以使用:
from <i>app_name</i>.models import <b>Todo</b>
def todo(request):
if request.method == 'POST':
title = request.POST.get('title')
description = request.POST.get('description')
todo_list = <b>Todo</b>.objects.create(
user=request.user,
title=title,
description= description
)
messages.success(request, 'Successfully Created')
return render(request, 'main/todo.html')
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
Note: It is better to use a Form
[Django-doc]
than to perform manual validation and cleaning of the data. A Form
will not
only simplify rendering a form in HTML, but it also makes it more convenient
to validate the input, and clean the data to a more convenient type.
我正在尝试添加待办事项,但当我尝试保存我的模型时,它说待办事项有一个意外的关键字参数。
TypeError at /
todo() got an unexpected keyword argument 'user'
它抛出以下错误
我的models.py
class todo(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
date= models.DateTimeField(auto_now = True)
is_completed = models.BooleanField(default=False)
def __str__(self):
return self.user.username + self.title
我的观点是这样的
def todo(request):
if request.method == "POST":
title = request.POST.get('title')
description = request.POST.get('description')
todo_list = todo(user=request.user, title=title, description= description)
todo_list.save()
messages.success(request, 'Successfully Created')
return render(request, 'main/todo.html')
请帮帮我
您的模型和视图都命名为 todo
,因此如果您在该视图中调用 todo
,该函数将覆盖模型,从而进行递归调用。
您可以使用不同的名称导入 todo
,例如:
from <i>app_name</i>.models import todo as todo_model
def todo(request):
if request.method == 'POST':
title = request.POST.get('title')
description = request.POST.get('description')
todo_list = <b>todo_model</b>.objects.create(
user=request.user,
title=title,
description= description
)
messages.success(request, 'Successfully Created')
return render(request, 'main/todo.html')
但是根据PEP-8 style guidelines,类写在PerlCase
,不是snake_case
。因此,您可能希望将模型重命名为:
class <b>Todo</b>(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
date= models.DateTimeField(auto_now = True)
is_completed = models.BooleanField(default=False)
def __str__(self):
return self.user.username + self.title
然后您可以使用:
from <i>app_name</i>.models import <b>Todo</b>
def todo(request):
if request.method == 'POST':
title = request.POST.get('title')
description = request.POST.get('description')
todo_list = <b>Todo</b>.objects.create(
user=request.user,
title=title,
description= description
)
messages.success(request, 'Successfully Created')
return render(request, 'main/todo.html')
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
Note: It is better to use a
Form
[Django-doc] than to perform manual validation and cleaning of the data. AForm
will not only simplify rendering a form in HTML, but it also makes it more convenient to validate the input, and clean the data to a more convenient type.