Django:Internet Explorer发布一个空表格

时间:2018-11-16 03:05:20

标签: python django forms internet-explorer

我有一个ModelForm,非常适合FireFox和Chrome,但是由于某些原因,每当有人使用IE提交表单时,服务器都会收到带有空值的表单。我已经通过在form.is_valid()中打印提交的值来验证了这一点(如下所示)。

这是我的代码:

template.html:

<form id="tracking_form" action="/tracking/{{ view.person.id }}" method="POST">
    {% csrf_token %}
    {{ form.person }}
    <input type="submit" class="btn btn-default" value="Add Data" style="width: 190px;">
</form>

forms.py:

class TrackingForm(forms.ModelForm):

    def __init__(self, active_student=None, *args, **kwargs):
        super(TrackingForm, self ).__init__(*args, **kwargs)
        points_dict = {'class': 'weekday_points', 'min': 0, 'max': 100 }
        arrived_dict = { 'class': 'not_required' }


        for field in self.fields.values():
            field.widget.attrs['form'] = 'tracking_form'
            field.widget.attrs['class'] = 'form_field'
            field.required = False

        self.fields['monday_points'].widget.attrs.update(points_dict)
        self.fields['tuesday_points'].widget.attrs.update(points_dict)
        self.fields['wednesday_points'].widget.attrs.update(points_dict)
        self.fields['thursday_points'].widget.attrs.update(points_dict)
        self.fields['friday_points'].widget.attrs.update(points_dict)
        self.fields['monday_arrived'].widget.attrs.update(arrived_dict)
        self.fields['tuesday_arrived'].widget.attrs.update(arrived_dict)
        self.fields['wednesday_arrived'].widget.attrs.update(arrived_dict)
        self.fields['thursday_arrived'].widget.attrs.update(arrived_dict)
        self.fields['friday_arrived'].widget.attrs.update(arrived_dict)

    def is_valid(self):
        # This is just here to print the values server receives
        for name, field in self.fields.items():

            if field.disabled:
                value = self.get_initial_for_field(field, name)
            else:
                value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
            print(name, ":", field.clean(value))

        valid = super(TrackingForm, self).is_valid()

        return valid


    class Meta:

        model = models.Tracking
        exclude = ('person',)

这是使用FireFox提交表单时的日志:

2018-11-16 02:23:15 week : 3
2018-11-16 02:23:15 term : 4
2018-11-16 02:23:15 year : 2018
2018-11-16 02:23:15 b1 : fasd
2018-11-16 02:23:15 b2 : asdfsad
2018-11-16 02:23:15 monday_points : 0
2018-11-16 02:23:15 monday_attended : 1
2018-11-16 02:23:15 monday_arrived : 09:00:00
2018-11-16 02:23:15 tuesday_points : 0
2018-11-16 02:23:15 tuesday_attended : 1
2018-11-16 02:23:15 tuesday_arrived : 09:00:00
2018-11-16 02:23:15 wednesday_points : 0
2018-11-16 02:23:15 wednesday_attended : 1
2018-11-16 02:23:15 wednesday_arrived : 10:30:00
2018-11-16 02:23:15 thursday_points : 0
2018-11-16 02:23:15 thursday_attended : 1
2018-11-16 02:23:15 thursday_arrived : 09:00:00
2018-11-16 02:23:15 friday_points : 0
2018-11-16 02:23:15 friday_attended : 1
2018-11-16 02:23:15 friday_arrived : 09:00:00

这是在IE中提交相同表单的日志:

2018-11-16 02:23:41 week : 
2018-11-16 02:23:41 term : 
2018-11-16 02:23:41 year : 
2018-11-16 02:23:41 b1 : 
2018-11-16 02:23:41 b2 : 
2018-11-16 02:23:41 monday_points : None
2018-11-16 02:23:41 monday_attended : 
2018-11-16 02:23:41 monday_arrived : 
2018-11-16 02:23:41 tuesday_points : None
2018-11-16 02:23:41 tuesday_attended : 
2018-11-16 02:23:41 tuesday_arrived : 
2018-11-16 02:23:41 wednesday_points : None
2018-11-16 02:23:41 wednesday_attended : 
2018-11-16 02:23:41 wednesday_arrived : 
2018-11-16 02:23:41 thursday_points : None
2018-11-16 02:23:41 thursday_attended : 
2018-11-16 02:23:41 thursday_arrived : 
2018-11-16 02:23:41 friday_points : None
2018-11-16 02:23:41 friday_attended : 
2018-11-16 02:23:41 friday_arrived : 

1 个答案:

答案 0 :(得分:1)

我修复了它。从我发布的代码来看还不是很清楚,但是表单的大部分输入都在标记之外。 IE显然无法弄清楚那些标签属于表单。我通过在所有输入标签周围放置表单标签来修复它。另一个解决方案可能是将form属性用于输入标签:

<form id="myform">
    <input id="something" type="text">
</form>

<button form="myform" type="submit">Submit that form over there</button>

但是我还没有验证这是否可行。