Note that there are some explanatory texts on larger screens.

plurals
  1. PORendering Admin Forms When An Error Occurs in Django
    primarykey
    data
    text
    <p>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:</p> <pre><code>Caught ValueError while rendering: need more than 0 values to unpack on line 19 9 {% for field in line %} 10 &lt;div{% if not line.fields|length_is:'1' %} class="field-box{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}&gt; 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 &lt;p&gt;{{ field.contents }}&lt;/p&gt; 18 {% else %} 19 {{ field.field }} 20 {% endif %} 21 {% endif %} 22 {% if field.field.field.help_text %} 23 &lt;p class="help"&gt;{{ field.field.field.help_text|safe }}&lt;/p&gt; 24 {% endif %} 25 &lt;/div&gt; 26 {% endfor %} 27 &lt;/div&gt; 28 {% endfor %} 29 &lt;/fieldset&gt; </code></pre> <p>Here is how my model looks like:</p> <pre><code>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) </code></pre> <p>And here is my custom clean() for my custom form:</p> <pre><code> 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 </code></pre> <p>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.</p> <p>Any ideas how I can render my form when there's an error in it?</p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload