当 Django 发生错误时渲染管理表单
我有一个模型,其中包含一个带有外键的字段,该字段可以为 NULL 并且是可选的。如果该字段没有值,我可以将此表单保存在管理页面中,不会出现任何问题。但是,当我的表单出现错误时,我的表单无法呈现,并且 Django 返回以下错误:
Caught ValueError while rendering: need more than 0 values to unpack on line 19
9 {% for field in line %}
10 <div{% if not line.fields|length_is:'1' %} class="field-box{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}>
11 {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12 {% if field.is_checkbox %}
13 {{ field.field }}{{ field.label_tag }}
14 {% else %}
15 {{ field.label_tag }}
16 {% if field.is_readonly %}
17 <p>{{ field.contents }}</p>
18 {% else %}
19 {{ field.field }}
20 {% endif %}
21 {% endif %}
22 {% if field.field.field.help_text %}
23 <p class="help">{{ field.field.field.help_text|safe }}</p>
24 {% endif %}
25 </div>
26 {% endfor %}
27 </div>
28 {% endfor %}
29 </fieldset>
这是我的模型的样子:
class Circuit(CommonFields):
"""Circuit class model."""
vid = models.AutoField(primary_key=True)
vname = models.CharField(
'Vendor Name',
max_length=100,
null=True,
blank=True)
vspc = models.ForeignKey(
Room,
db_column='avspc',
verbose_name='Space',
null=True,
blank=True)
这是我的自定义表单的自定义 clean():
def clean(self):
"""Custom port range validator."""
super(CircuitForm, self).clean()
raise forms.ValidationError("Some stupid error just happened.")
cleaned_data = self.cleaned_data
return cleaned_data
请注意,如果我删除 raise 语句,我的表单可以成功提交,但是,当我提出错误时,它给出了上述错误。
当表单出现错误时,我有什么想法如何呈现表单?
请注意,仅当我在未设置可选字段的情况下创建新条目或编辑未设置可选字段的条目时,才会发生此错误。如果我正在编辑设置了这些可选字段的现有条目,如果我取消设置可选字段,则不会给出任何错误。
I have a model that contains a field with a foreign key and this field can be NULL and is optional. I can save this form in admin page without any problems if this field has no values. However, when there's an error on my form, my form cannot be rendered and Django returns the following error:
Caught ValueError while rendering: need more than 0 values to unpack on line 19
9 {% for field in line %}
10 <div{% if not line.fields|length_is:'1' %} class="field-box{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}>
11 {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12 {% if field.is_checkbox %}
13 {{ field.field }}{{ field.label_tag }}
14 {% else %}
15 {{ field.label_tag }}
16 {% if field.is_readonly %}
17 <p>{{ field.contents }}</p>
18 {% else %}
19 {{ field.field }}
20 {% endif %}
21 {% endif %}
22 {% if field.field.field.help_text %}
23 <p class="help">{{ field.field.field.help_text|safe }}</p>
24 {% endif %}
25 </div>
26 {% endfor %}
27 </div>
28 {% endfor %}
29 </fieldset>
Here is how my model looks like:
class Circuit(CommonFields):
"""Circuit class model."""
vid = models.AutoField(primary_key=True)
vname = models.CharField(
'Vendor Name',
max_length=100,
null=True,
blank=True)
vspc = models.ForeignKey(
Room,
db_column='avspc',
verbose_name='Space',
null=True,
blank=True)
And here is my custom clean() for my custom form:
def clean(self):
"""Custom port range validator."""
super(CircuitForm, self).clean()
raise forms.ValidationError("Some stupid error just happened.")
cleaned_data = self.cleaned_data
return cleaned_data
Note that if I remove raise statement, my forms can be submitted successfully, however, when I raise an error, it gives me the above error.
Any ideas how I can render my form when there's an error in it?
Note that this error occurs only when I'm creating a new entry without setting the optional field or when editing an entry that doesn't have the optional fields set. If I'm editing an existing entry which has those optional field set, if I unset the optional fields, it doesn't give any error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@GeoffroyCALA 是对的,你需要条件来引发异常。如果您不检查条件,无论如何都会引发异常。此外,您没有指定为其设置自定义例外的表单字段。请参阅文档示例 。
尝试这样的事情:
@GeoffroyCALA is right, you need condition to raise an exception. If you do not check the condition, exception will be raised anyway. Moreover, you do not specify a form field for which you set the custom exception. See the docs example.
Try something like that: