为什么在加载这个Django模板时我得到“IndexError:字符串索引超出范围”?

时间:2014-07-03 06:33:18

标签: django django-templates

>>> from factories import PageFactory
>>> p = PageFactory.create()
>>> p.name
u'Hey There, Im Jack697'
>>> from django.template import Template, Context
>>> t = Template("{% load inplace_edit %} {% inplace_edit page.name %}")
>>> t.render(Context({'page': p}))
Traceback (most recent call last):
File "<input>", line 1, in <module>
  File "lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "lib/python2.7/site-packages/inplaceeditform/tag_utils.py", line 103, in render
    context = self.prepare_context(args, kwargs, context)
  File "lib/python2.7/site-packages/inplaceeditform/templatetags/inplace_edit.py", line 99, in prepare_context
    obj = Variable(obj_context).resolve(context)
  File "lib/python2.7/site-packages/django/template/base.py", line 714, in __init__
    self.literal = mark_safe(unescape_string_literal(var))
  File "lib/python2.7/site-packages/django/utils/functional.py", line 196, in wrapper
    return func(*args, **kwargs)
  File "lib/python2.7/site-packages/django/utils/text.py", line 413, in unescape_string_literal
    if s[0] not in "\"'" or s[-1] != s[0]:

inplace_edit标记,这是第三方包:

class InplaceEditNode(RenderWithArgsAndKwargsNode):

    def prepare_context(self, args, kwargs, context):
        expression_to_show = args[0]
        tokens_to_show = expression_to_show.split('|')
        obj_field_name, filters_to_show = tokens_to_show[0], '|'.join(tokens_to_show[1:])
        obj_field_name_split = obj_field_name.split(".")
        obj_context = '.'.join(obj_field_name_split[:-1])
        field_name = obj_field_name_split[-1]
        obj = Variable(obj_context).resolve(context)
        adaptor = kwargs.get('adaptor', None)
        class_adaptor = get_adaptor_class(adaptor, obj, field_name)
        request = context.get('request')

        config = class_adaptor.get_config(request, **kwargs)

        adaptor_field = class_adaptor(request, obj, field_name,
                                      filters_to_show,
                                      config)

        context = {
            'adaptor_field': adaptor_field,
        }
        return context


@register.tag
def inplace_edit(parser, token):
    args, kwargs = parse_args_kwargs(parser, token)
    return InplaceEditNode(args, kwargs, 'inplaceeditform/inplace_edit.html')

此模板标记在其他模板中使用相同的Page模型。我基本上想知道“Django模板标签会出现这样的常见原因吗?”

1 个答案:

答案 0 :(得分:1)

documentation您可以看到需要引用标签的参数。

{% inplace_edit  "OBJ.FIELD_NAME" %}
{% inplace_edit  "OBJ.FIELD_NAME|FILTER1|FILTER2|...|FILTERN" %}

引用参数将解决问题。