xadmin:list index out of range

Posted hehecat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xadmin:list index out of range相关的知识,希望对你有一定的参考价值。

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分割’ ‘,然而为什么没有分割成功。

input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('
') 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.

被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

以上是关于xadmin:list index out of range的主要内容,如果未能解决你的问题,请参考以下文章

解决Python3.6.5+Django2.0集成xadmin后台点击添加或者内容详情报 list index out of range 的错误

list assignment index out of range

Python常见错误:IndexError: list index out of range

list index out of bounds

IndexError: list index out of range list超出了?要怎么改?

关于python list index out of range