xadmin:list index out of range

时间:2021-06-27 16:45:07
Request Method:     GET
Request URL:    http://127.0.0.1:8000/xadmin/goods/goodscategory/add/
Django Version:     1.11.10
Exception Type:     IndexError
Exception Value:    

list index out of range

Exception Location:     /home/hehecat/PycharmProjects/MxShop/extra_apps/xadmin/widgets.py in render, line 81
Python Executable:  /home/hehecat/anaconda3/envs/restful/bin/python
Python Version:     3.6.6
Python Path:    

['/home/hehecat/PycharmProjects/MxShop/extra_apps',
 '/home/hehecat/PycharmProjects/MxShop/apps',
 '/home/hehecat/PycharmProjects/MxShop',
 '/home/hehecat/PycharmProjects/MxShop',
 '/home/hehecat/PycharmProjects/MxShop',
 '/home/hehecat/PycharmProjects/MxShop/apps',
 '/home/hehecat/PycharmProjects/MxShop/extra_apps',
 '/home/hehecat/anaconda3/envs/restful/lib/python36.zip',
 '/home/hehecat/anaconda3/envs/restful/lib/python3.6',
 '/home/hehecat/anaconda3/envs/restful/lib/python3.6/lib-dynload',
 '/home/hehecat/anaconda3/envs/restful/lib/python3.6/site-packages',
 '/home/hehecat/CodePro/pycharm-2018.1.1/helpers/pycharm_matplotlib_backend']

Server time:    星期二, 17 七月 2018 13:41:47 +0800

按提示到xadmin/widget.py看看

...
return mark_safe('<div class="datetime clearfix"><div class="input-group date bootstrap-datepicker"><span class="input-group-addon"><i class="fa fa-calendar"></i></span>%s'
                                '<span class="input-group-btn"><button class="btn btn-default" type="button">%s</button></span></div>'
                             '<div class="input-group time bootstrap-clockpicker"><span class="input-group-addon"><i class="fa fa-clock-o">'
                             '</i></span>%s<span class="input-group-btn"><button class="btn btn-default" type="button">%s</button></span></div></div>' % (input_html[0], _(u'Today'), input_html[1], _(u'Now')))
...

81行 是

'</i></span>%s<span class="input-group-btn"><button class="btn btn-default" type="button">%s</button></span></div></div>' % (input_html[0], _(u'Today'), input_html[1], _(u'Now')))
Variable Value
class <class 'xadmin.widgets.AdminSplitDateTime'>
attrs {'id': 'id_add_time', 'required': True}
input_html ['<input type="text" name="add_time_0" value="2018/07/17" class="date-field ' 'admindatewidget" size="10" required id="id_add_time_0" /><input type="text" ' 'name="add_time_1" value="13:41" class="time-field admintimewidget" size="8" ' 'required id="id_add_time_1" />']
name 'add_time'
self <xadmin.widgets.AdminSplitDateTime object at 0x7f4a4632f7f0>
value datetime.datetime(2018, 7, 17, 13, 41, 47)

在报错页面的81行 Local var可以看到input_html只有1个元素,input_html[1]越界了。

但在input_html变量里有两个input标签,所以其本意应该是将两个input分割出来。

在76行使用了split分割’ \n‘,然而为什么没有分割成功。

input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('\n') if ht != '']
forms.MultiWidget.__init__(self, widgets, attrs)

在__init__中可以看到使用MultiWidget对这里得html进行了渲染

MultiWidget.html

{% spaceless %}{% for widget in widget.subwidgets %}{% include widget.template_name %}{% endfor %}{% endspaceless %}

spaceless

Removes whitespace between HTML tags. This includes tabcharacters and newlines.

\n被spaceless标签删了,那就换种分割方式。

input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('/><') if ht != '']
            input_html[0] = input_html[0] + "/>"
            input_html[1] = "<" + input_html[1]

参考:https://blog.csdn.net/yuhan963/article/details/79167743