I am writing a custom test case for Django-Mptt template tag . Eveything is working fine but the json I am getting is not valid .
this is the template string I am sending :
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 %}
]''')
output :
[{"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": []}]}]}]
If u use any json validator for above o/p then u will know about my error in json output , there must be a comma b/w to comments to make it valid.
Please let me know the solution how I can make this json valid
the solution is join comments by comma ','
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 ,**
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.