- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建一个模型:
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/
内部 TransactionScope 将哪个事务作为环境事务? using ( var t1 = new TransactionScope( TransactionScopeOption.Requi
我需要匹配以下模式:N.N.N 324324.234324.234324 匹配,以及 1.1.1 I have the following pattern: (\d*\.\d*\.\d*) 问题是,它
这个问题在这里已经有了答案: HTML5 required attribute not working (2 个答案) 关闭上个月。 我最近为我创建了一个网页来学习更多关于 HTML 的知识,我发现
我在 NgForm 中有以下字段: 0'" /> Dependency is required 问题是这个字段总是需要的,不管 [required]相关条件depSelected>0 . d
我有一个模型类,其中包含几个必填字段: public class UserMetadata { [Required(ErrorMessage = "Please enter a name.")]
我有一张预订表格,需要验证。某些字段的启用/禁用取决于之前选择的选项。我的问题是,我无法提交禁用字段的表单,因为它等待来自空字段的“有效”输入数据。 是否有一种方法可以仅在启用字段集时启用/禁用这些字
如何为textarea设置两个属性(默认文本,点击后隐藏,必填)? Default text HTML“认为”默认文本是我的输入。我该如何修复它?谢谢大家。 最佳答案 如果您不介意兼容性,简单
我是编码新手,所以这可能是一个非常简单的问题。 编译时出现错误: GradeAnalyzer.java:49: error: method getAverage in class GradeAnaly
我有一个带有 2 个输入字段的 from:手机和电话。我希望至少需要其中一个字段,以便可以提交表单。要么您输入手机号码,不再需要电话输入,要么反之亦然。 我发现 Jquery 验证器有条件语句,
看看下面的代码,我正在使用 required(System.ComponentModel.DataAnnotations) 命名空间,但它一直向我显示红色波浪线,并显示无法找到“type or nam
所以我有一个 knockout 原型(prototype),您可以在其中动态添加输入,然后设置每个设置。将其视为表单生成器就是这样。然而,我注意到禁用和必需的效果不太好。它将值设置为禁用或必需,但是当
我正在使用 Entity Framework 的表拆分功能来拆分我的实体数据模型,如下所示: +--------+ +--------------+ | News | | NewsI
我有 3 个关于 TransactionScopeOption 的问题。Required 把我逼疯了,我无法在网上找到他们的答案。 A. 我很难思考什么时候我必须在现实中编写这段代码?为什么我不应该将
public static int biggestArrayGap(int []a, int n) { int biggestGap = Math.abs(a[1]-a[0]); for (i
我真的发现 django 表单集令人困惑。 我尤其对以下我不太了解的概念有疑问: The formset is smart enough to ignore extra forms that were
在我的 Angular v6 应用程序中,我尝试显示一个下拉列表,并根据 bool 值将其设置为必需,该 bool 值设置在复选框的值上。以下是我的模板中的代码片段(includeModelVersi
我正在使用jquery validation插入。当我使用 required( dependency-expression ) 时,我注意到 required( dependency-expressi
代码: ArrayList marks = new ArrayList(); String output = "Class average:" + calculateAverage() + "\
static void sort (Textbook [ ] info, int numEntries) { long tempIsbn = 0; String tempTitle =
在我的 Angular v6 应用程序中,我尝试显示一个下拉列表,并根据 bool 值将其设置为必需,该 bool 值设置在复选框的值上。以下是我的模板中的代码片段(includeModelVersi
我是一名优秀的程序员,十分优秀!