views:

253

answers:

1

I've been looking for a way to create a read-only form field and every article I've found on the subject comes with a statement that "this is a bad idea". Now for an individual form, I can understand that there are other ways to solve the problem, but using a read only form field in a modelformset seems like a completely natural idea.

Consider a teacher grade book application where the teacher would like to be able to enter all the students' (note the plural students) grades with a single SUBMIT. A modelformset could iterate over all the student-grades in such a way that the student name is read-only and the grade is the editable field. I like the power and convenience of the error checking and error reporting you get with a modelformset but leaving the student name editable in such a formset is crazy.

Since the expert django consensus is that read-only form fields are a bad idea, I was wondering what the standard django best practice is for the example student-grade example above?

+1  A: 

The reason you don't want to do this is because someone can change your disabled field to enabled and then submit the form. You would have to change the save function as to not insert the "disabled" data.

The standard way to do this is to not put the name in an input, but to display it as text

<form>
    <div>
        <label>Name</label>
        <p>Johnny Five</p>
    </div>
    <div>
        ....

This is not possible in django.

I say if you really trust your userbase to not "mess" with things then go for it, but if its a public facing website with possible sensitive data then stay away.

Galen
Thanks Galen that explains most of the concern, but I'm still curious how (or if) folks would use Django to implement the grade book page I suggested above. Abandon the built-in tools?
jamida
check out this answerhttp://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-be/325038#325038
Galen
A fair number of people reading the request for "read-only" interpret it literally as in adding a "readonly" or "disabled" attribute to the widget. That has ALL the problems you spoke of above (and will work in a pinch) but ideally there'd be other solutions like your <p> (or <span>) tag above. I'm going to try this next: http://lazypython.blogspot.com/2008/12/building-read-only-field-in-django.html
jamida
right the disabled input is dangerous like i said, i thought you were asking for the solution anyway.
Galen