[Fixed]-Only display field value from a class form in Django

1👍

To make c un-editable, I would use the Field.disabled attribute new in Django 1.9. See this post for information on how to use it and what to do if you’re in 1.8 or older:

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

As far as the saving issue goes, I would have to see your view to be sure but it sounds like something may be going awry during form validation. Your form will expect all three fields since that’s how it’s initialized, but your template can remove a field independent of the view logic. Perhaps the issue crops up when only two of the three expected fields go through form.is_valid()?

Would it be acceptable for c to always be exposed in the page source? You could render the form normally in all cases, then check the value of field b using jQuery/JS and conditionally .hide it.

Alternatively, you could check your model’s attributes in the view and conditionally remove a field from the form’s self.fields dict or override the init method.

Leave a comment