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')))
VariableValue
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>
valuedatetime.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

转载于:https://www.cnblogs.com/hehecat/p/9323234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值