无法将表单字段设为只读

时间:2019-04-04 13:58:52

标签: python django django-forms django-views

我有一个表格。它具有字段“ first_term_comment”。 我正在尝试将此字段设为只读。但这并不是使其变为只读。

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

如果我使用禁用的属性,它将起作用。但这不是我所需要的。我需要将其设置为只读。对象类型为: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})

0 个答案:

没有答案