[英]Invalid json in Django-mptt model
我正在为Django-Mptt template tag编写自定义测试用例。 Eveything工作正常,但是我得到的json无效。
这是我发送的模板字符串:
self.template = re.sub(r'(?m)^[\s]+', '', '''
{% load yctags %}
[
{% recursetree_custom comments comment_order %}
{
"comment_id": "{{ node.id }}",
"comment_text": "{{ node.text }}",
"comment_parent_id": "{{ node.parent.id }}",
"children": [
{% if not node.is_leaf_node %}
{{ children }}
{% endif %}
]
}
{% endrecursetree %}
]''')
输出:
[{"comment_id": "2","comment_text": "2nd comment","comment_parent_id": "","children": [{"comment_id": "7","comment_text": "2nd comment , 2nd child","comment_parent_id": "2","children": []}{"comment_id": "6","comment_text": "2nd comment , ist child","comment_parent_id": "2","children": []}]}{"comment_id": "1","comment_text": "Ist comment","comment_parent_id": "","children": [{"comment_id": "3","comment_text": "Ist comment , ist child","comment_parent_id": "1","children": [{"comment_id": "5","comment_text": "1-1-2","comment_parent_id": "3","children": []}{"comment_id": "4","comment_text": "1-1-1","comment_parent_id": "3","children": []}]}]}]
如果您对上述o / p使用任何json验证器,则您将知道我在json输出中的错误,注释中必须有一个逗号黑白以使其有效。
请让我知道解决方案,如何使此json有效
解决方案是用逗号“,”加入注释
def _render_node(self, context, node):
# print "node-> ",node
bits = []
context.push()
#print "context->", context
for child in node.get_children():
bits.append(self._render_node(context, child))
context['node'] = node
context['children'] = mark_safe(','.join(bits)) **#join by ,**
rendered = self.template_nodes.render(context)
context.pop()
return rendered
def render(self, context):
queryset = self.queryset_var.resolve(context)
comment_order = self.order.resolve(context)
roots = cache_tree_children(queryset, comment_order)
# print "roots-> ", roots
bits = [self._render_node(context, node) for node in roots]
# print bits
return ','.join(bits) **#join by ,**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.