1👍
✅
This is a known issue in Crispy Forms – see #326, and #257 which is the root of the issue.
The readonly
attribute is a boolean attribute as opposed to a key-value attribute, i.e., you can use it like this:
<input name='foo' readonly>
The presence of the attribute means that the field is readonly.
Crispy forms does not handle such boolean attributes (with the exception of the required
attribute which is has a special case for) and just renders them as it would any other attribute.
This is a problem because as far as your browser is concerned, readonly="true"
and readonly="false"
are the same thing. The mere presence of that attribute will cause the field to be read-only.
You could do something like this as a workaround:
self.helper.layout = Layout(
Field('contact_email', readonly=True) if email_readonly else Field('contact_email'),
)
Source:stackexchange.com