gpt4 book ai didi

django - save() 缺少 1 个必需的位置参数 : 'request' in django

转载 作者:行者123 更新时间:2023-12-03 09:12:50 24 4
gpt4 key购买 nike

我创建一个模型:

class Person(models.Model):
name = models.CharField(max_length=250)
slug = AutoSlugField(populate_from='name')
birth_date = models.DateField(null=True, blank=True)
blood_group = models.CharField(max_length=5)
present_address = models.CharField(max_length=250, blank=True)
permanent_address = models.CharField(max_length=250, blank=True)
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='member_persons')

表单.py:

class MemberForm(ModelForm):

class Meta:
model = Person
exclude = ('user',)

def save(self, request, commit=True):
person = super().save(commit=False)
if not person.pk:
person.user = get_user(request)
if commit:
person.save()
self.save_m2m()
return person

对于第一人称创作来说效果很好。当同一个人再次尝试提交具有不同数据的创建表单时,它会给出“save()”缺少 1 个必需的位置参数:“请求”。完整回溯:

Traceback:

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
217. return super(BaseCreateView, self).post(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
183. return self.form_valid(form)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
162. self.object = form.save()

Exception Type: TypeError at /person/create/
Exception Value: save() missing 1 required positional argument: 'request'

我的观点.py:

@require_authenticated_permission(
'member.add_person')
class PersonCreate(CreateView):
template_name = 'member/person_form.html'
model = Person
success_url = '/person/'
form_class = MemberForm

我想显示一条消息“您已经提交了数据”,而不是收到此错误。如何在人员创建表单中显示此消息?任何帮助将不胜感激。

编辑:

新错误:

回溯:

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get
213. return super(BaseCreateView, self).get(request, *args, **kwargs)

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get
174. return self.render_to_response(self.get_context_data())

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get_context_data
93. kwargs['form'] = self.get_form()

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get_form
45. return form_class(**self.get_form_kwargs())

Exception Type: TypeError at /person/create/
Exception Value: __init__() got an unexpected keyword argument 'request'

最佳答案

save 签名无效。参数中不应有 request。如果您想将其传递给表单,请使用 get_form

@require_authenticated_permission('member.add_person')
class PersonCreate(CreateView):
template_name = 'member/person_form.html'
model = Person
success_url = '/person/'
form_class = MemberForm

def get_form(self, form_class=None):
form = super().get_form(form_class)
form.request = self.request
return form

然后

class MemberForm(ModelForm):

class Meta:
model = Person
exclude = ('user',)

def save(self, commit=True):
person = super().save(commit=False)
if not person.pk:
person.user = get_user(self.request)
if commit:
person.save()
self.save_m2m()
return person

关于django - save() 缺少 1 个必需的位置参数 : 'request' in django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40394448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com