0

I have a formset. It has a field "first_term_comment". I am trying to make this field readonly. But it is not making it readonly.

if today < semester.first_term_first_exam_begin_date:
       form.fields["first_term_comment"].widget.attrs['readonly'] = True

If i use disabled attribute it works. But this is not what i need. I need to make it readonly. The object type is : django.forms.models.ModelChoiceField

    for form in formset:
        form.fields["student"].widget = PictureWidget()
        form.fields["inform_report_comment"].widget.attrs['size'] = 250
        form.fields["first_term_comment"].widget.attrs['size'] = 50
        form.fields["second_term_comment"].widget.attrs['size'] = 50
        if form.initial:
            form.fields["student"].queryset = Student.objects.filter(std_no=int(form.initial["student"]))
        if not request.user.is_superuser:
            today = nicosia_date(datetime.today()).date()
            grade_field_names = [
                'first_term_first_exam', 'first_term_second_exam', 'first_term_homework',
                'second_term_first_exam', 'second_term_second_exam', 'second_term_homework',
                'resit_exam',
            ]
            for field_name in grade_field_names:
                if today < semester.__getattribute__("%s_begin_date" % field_name) or today > semester.__getattribute__("%s_ending_date" % field_name):
                    form.fields[field_name].widget = forms.TextInput()
                    form.fields[field_name].widget.attrs['readonly'] = True

            # If today is out of first semester start or after resit make
            # comment fields read only
            if today < semester.first_term_first_exam_begin_date or today > semester.resit_exam_ending_date:
                for field_name in ('first_term_comment', 'second_term_comment', 'inform_report_comment'):
                    form.fields[field_name].widget.attrs['readonly'] = True

            #if (today < min([semester.first_term_first_exam_begin_date, semester.first_term_second_exam_begin_date, semester.first_term_homework_begin_date])) or (today > max([semester.first_term_first_exam_ending_date, semester.first_term_second_exam_ending_date, semester.first_term_homework_ending_date])):
            #    form.fields["first_term_comment"].widget = forms.TextInput()
            #    form.fields["first_term_comment"].widget.attrs['readonly'] = True
            #if (today < min([semester.second_term_first_exam_begin_date, semester.second_term_second_exam_begin_date, semester.second_term_homework_begin_date])) or (today > max([semester.second_term_first_exam_ending_date, semester.second_term_second_exam_ending_date, semester.second_term_homework_ending_date])):
            #    form.fields["second_term_comment"].widget = forms.TextInput()
            #    form.fields["second_term_comment"].widget.attrs['readonly'] = True

        #form.fields["first_term_comment"].widget = forms.TextInput()
        form.fields["first_term_comment"].widget.attrs['readonly'] = True


        #form.fields["second_term_comment"].widget = forms.TextInput()
        form.fields["second_term_comment"].widget.attrs['readonly'] = True



        form.fields["first_term_gradebook"].widget = forms.TextInput()
        form.fields["first_term_gradebook"].widget.attrs['readonly'] = True
        form.fields["second_term_gradebook"].widget = forms.TextInput()
        form.fields["second_term_gradebook"].widget.attrs['readonly'] = True
        form.fields["course"].widget = forms.HiddenInput()
        form.fields["student"].widget.attrs['style'] = "width:300px;"
        form.fields["uuid"].widget = forms.HiddenInput()
        form.fields["gradebook_average"].widget = forms.TextInput()
        form.fields["gradebook_average"].widget.attrs['readonly'] = True
    helper = GradesInlineHelper()
    return render(request, 'create-grades.html', {'formset': formset, 'cls': course_teacher.cls, 'helper': helper, 'course_teacher_pk_uuid': course_teacher_pk_uuid, 'course_title': course_title})
ivbtar
  • 799
  • 11
  • 29
  • Add the whole form class. Also, which django version are you using? – gdef_ Apr 04 '19 at 14:05
  • django 1.9.5, added whole code for form – ivbtar Apr 04 '19 at 14:11
  • What is the difference between disabled and readonly? In [django docs](https://docs.djangoproject.com/en/dev/ref/forms/fields/#disabled) a disabled field "will not be editable by others". Isn't this what you want? You may also [check this post](https://stackoverflow.com/a/325038/2996101) – raratiru Apr 04 '19 at 14:18
  • No if i use a form field as disabled the field value will not be posted. But readonly form field values are posted. – ivbtar Apr 04 '19 at 14:22
  • Oh, I see. The idea is that django will not accept the value of a disabled field posted because in case of an error the user will not be able to change the value and correct it. A workaround is to use [`forms.widgets.HiddenInput`](https://docs.djangoproject.com/en/dev/ref/forms/widgets/#hiddeninput) and print the value in the form somehow. But generally submitting a hidden or disabled value is not considered a good practice. – raratiru Apr 04 '19 at 14:26
  • Another idea you could implement is to allow the field to be editable when creating a new object, and make it disabled when retrieving an existing object from the database. – raratiru Apr 04 '19 at 14:34

0 Answers0