Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As pointed out in <a href="https://stackoverflow.com/questions/324477/in-a-django-form-how-do-i-make-a-field-readonly-or-disabled-so-that-it-cannot/34538169#34538169">this answer</a>, Django 1.9 added the <a href="https://docs.djangoproject.com/en/1.9/ref/forms/fields/#disabled" rel="noreferrer">Field.disabled</a> attribute:</p> <blockquote> <p>The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in favor of the value from the form’s initial data.</p> </blockquote> <p>With Django 1.8 and earlier, to disable entry on the widget and prevent malicious POST hacks you must scrub the input in addition to setting the <code>readonly</code> attribute on the form field:</p> <pre><code>class ItemForm(ModelForm): def __init__(self, *args, **kwargs): super(ItemForm, self).__init__(*args, **kwargs) instance = getattr(self, 'instance', None) if instance and instance.pk: self.fields['sku'].widget.attrs['readonly'] = True def clean_sku(self): instance = getattr(self, 'instance', None)   if instance and instance.pk:     return instance.sku   else:     return self.cleaned_data['sku'] </code></pre> <p>Or, replace <code>if instance and instance.pk</code> with another condition indicating you're editing. You could also set the attribute <code>disabled</code> on the input field, instead of <code>readonly</code>.</p> <p>The <code>clean_sku</code> function will ensure that the <code>readonly</code> value won't be overridden by a <code>POST</code>.</p> <p>Otherwise, there is no built-in Django form field which will render a value while rejecting bound input data. If this is what you desire, you should instead create a separate <code>ModelForm</code> that excludes the uneditable field(s), and just print them inside your template.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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