Python save()缺少1个必需的位置参数:';自我';在django博客中
我正在学习Django,并在一个评论系统上工作 模型 意见 当我尝试创建新注释时,会发生此错误Python save()缺少1个必需的位置参数:';自我';在django博客中,python,django,django-views,Python,Django,Django Views,我正在学习Django,并在一个评论系统上工作 模型 意见 当我尝试创建新注释时,会发生此错误 TypeError at /blog/2019/10/29/my-second-post/ save() missing 1 required positional argument: 'self' Request Method: POST Request URL: http://127.0.0.1:8000/blog/2019/10/29/my-second-post/ Django Vers
TypeError at /blog/2019/10/29/my-second-post/
save() missing 1 required positional argument: 'self'
Request Method: POST
Request URL: http://127.0.0.1:8000/blog/2019/10/29/my-second-post/
Django Version: 2.2.6
Exception Type: TypeError
Exception Value:
save() missing 1 required positional argument: 'self'
Exception Location: E:\v_envs\elkhashen\src\blog\views.py in post_detail, line 35
Python Executable: E:\v_envs\elkhashen\Scripts\python.exe
Python Version: 3.7.4
Python Path:
['E:\\v_envs\\elkhashen\\src',
'E:\\v_envs\\elkhashen\\Scripts\\python37.zip',
'E:\\v_envs\\elkhashen\\DLLs',
'E:\\v_envs\\elkhashen\\lib',
'E:\\v_envs\\elkhashen\\Scripts',
'c:\\users\\elkhashen\\appdata\\local\\programs\\python\\python37-32\\Lib',
'c:\\users\\elkhashen\\appdata\\local\\programs\\python\\python37-32\\DLLs',
'E:\\v_envs\\elkhashen',
'E:\\v_envs\\elkhashen\\lib\\site-packages']
Server time: Thu, 31 Oct 2019 09:50:45 +0000
if request.method==“POST”:
new_comment=CommentForm(request.POST)
如果CommentForm.u有效:
new_comment=CommentForm.save(commit=False)在保存(commit=True)时,表单中的实例需要知道模型中的实例
只有在使用save(commit=False)时才需要调用save_m2m()。在表单上使用简单的save()时,所有数据(包括多对多数据)都会被保存,而无需任何其他方法调用。例如:
# Create a form instance with POST data.
>>> a = Author()
>>> f = AuthorForm(request.POST, instance=a) <-- Set the instance model here
# Create and save the new author instance. There's no need to do anything else.
>>> new_author = f.save()
#使用POST数据创建表单实例。
>>>a=作者()
>>>f=AuthorForm(request.POST,instance=a)我修复了一些语法和格式new\u comment=new\u comment.save(commit=False)
TypeError at /blog/2019/10/29/my-second-post/
save() missing 1 required positional argument: 'self'
Request Method: POST
Request URL: http://127.0.0.1:8000/blog/2019/10/29/my-second-post/
Django Version: 2.2.6
Exception Type: TypeError
Exception Value:
save() missing 1 required positional argument: 'self'
Exception Location: E:\v_envs\elkhashen\src\blog\views.py in post_detail, line 35
Python Executable: E:\v_envs\elkhashen\Scripts\python.exe
Python Version: 3.7.4
Python Path:
['E:\\v_envs\\elkhashen\\src',
'E:\\v_envs\\elkhashen\\Scripts\\python37.zip',
'E:\\v_envs\\elkhashen\\DLLs',
'E:\\v_envs\\elkhashen\\lib',
'E:\\v_envs\\elkhashen\\Scripts',
'c:\\users\\elkhashen\\appdata\\local\\programs\\python\\python37-32\\Lib',
'c:\\users\\elkhashen\\appdata\\local\\programs\\python\\python37-32\\DLLs',
'E:\\v_envs\\elkhashen',
'E:\\v_envs\\elkhashen\\lib\\site-packages']
Server time: Thu, 31 Oct 2019 09:50:45 +0000
# Create a form instance with POST data.
>>> a = Author()
>>> f = AuthorForm(request.POST, instance=a) <-- Set the instance model here
# Create and save the new author instance. There's no need to do anything else.
>>> new_author = f.save()