无法从 django MultiWidget 访问所有值,从请求。发布?



我不确定为什么在发布请求后无法从MutliWidget访问这两个字段。我尝试过各种方法,比如request.POST.get('name'),它只返回0。我也尝试过访问value_from_datadict(self, data, files, name)中的值,但没有成功。

最让我困惑的是,当我使用print(request.POST)时,我可以看到post请求中的表单确实包含了MultiWidget的两个值,但我只能访问一个

例如当我打印请求时。POST,打印到控制台:

<QueryDict: {'total_travel_time': ['0', '0']}>

但是当我尝试:request.POST.get('total_travel_time')时,我只接收到0

这里发生了什么??????这让我有点疯狂。

这是我的MutliWidget。

class TotalTimeWidget(forms.widgets.MultiWidget):
CON_STYLE = "margin-left: 0.25in;"
INPUT_STYLE = "width: .5in; display: inline; " 
"vertical-align: top; " 
"margin-left: 0.0675in; " 
"margin-right: 0.0675in;"
hour_attrs = dict()
min_attrs = dict()
name = ''
def value_from_datadict(self, data, files, name):
data = super(TotalTimeWidget, self).value_from_datadict(data, files, name)
print(data)
return data
def decompress(self, value):
print("decompress_called", value)
if value:
return value
else:
return ['', '']
def __init__(self, name, attrs=None, hours=0, minutes=0):
hour_attrs = {'min': 0, 'max': 12, 'class': "hour", 'value': hours}
min_attrs = {'min': 0, 'max': 59, 'class': "minute", 'value': minutes}
self.hour_attrs = hour_attrs
self.min_attrs = min_attrs
self.name = name
_widgets = (
widgets.NumberInput(attrs=hour_attrs),
widgets.NumberInput(attrs=min_attrs),
)
super(TotalTimeWidget, self).__init__(_widgets, attrs)
def set_initial(self, **kwargs):
if 'hours' in kwargs:
self.hour_attrs['value'] = kwargs['hours']
if 'minutes' in kwargs:
self.min_attrs['value'] = kwargs['minutes']
def render(self, name, value, attrs=None, renderer=None):
super(TotalTimeWidget, self).render(name, value, attrs, renderer)
html = '<div style="%(container_style)s"><span>Hours</span>'
html += '<input type="number" name="%(name)s" min="0" max="99" ' 
'class="hour" value="%(hour)s" style="%(style)s"' 
'required id="id_total_hours_%(name)s" />'
html += '<span>:</span>'
html += '<input type="number" name="%(name)s" min="0" max="59"' 
' class="hour" value="%(min)s" style="%(style)s" ' 
'required id="id_total_minutes_%(name)s" />'
html += '<span>Minutes</span></div>'
html = html % dict(hour=self.hour_attrs['value'],
min=self.min_attrs['value'],
name=name,
container_style=self.CON_STYLE,
style=self.INPUT_STYLE)
return mark_safe(html)

这是我使用MultiWidget 的字段

class TotalTimeField(forms.MultiValueField):
def __init__(self, *args, **kwargs):
hours = 0
minutes = 0
if 'hours' in kwargs:
hours = kwargs['hours']
del kwargs['hours']
if 'minutes' in kwargs:
minutes = kwargs['minutes']
del kwargs['minutes']
self.widget = TotalTimeWidget(name='total_time', hours=hours, minutes=minutes)
list_fields = [
forms.IntegerField(),
forms.IntegerField(),
]
super(TotalTimeField, self).__init__(list_fields, *args, **kwargs)
def compress(self, data_list):
print('Compress called', data_list)
if data_list:
return " ".join(data_list)

以下是视图:

def test_view(request):
form = ILSTimeAndServiceTrackingForm()
data = dict()
data['participant'] = Participant.objects.get_participant_by_id(2)
if request.POST:
print(request.POST)
print(request.POST.get('total_travel_time'))
data['form'] = form
return render(request, '../templates/tests/widget_test.html', data)

对于多个值有一种特殊的方法:QueryDict.getlist()

尝试从以下位置更改代码:

print(request.POST.get('total_travel_time'))

至:

print(request.POST.getlist('total_travel_time'))

如果有帮助,请告诉我们。


由于QueryDict.__getitem__QueryDict.get的文档显示:

如果键有多个值,则返回最后一个值。

相关内容

  • 没有找到相关文章

最新更新