How do you use links in a json template
How do you use links in a json template
Hey, I am trying to set up a table in django using datatables. I am getting my data from a very large mongo database. For this reason, I am trying to do things server side. I am looking at the example here: http://www.assembla.com/code/datatables_demo/subversion/nodes/trunk/1_6_2/datatables_demo?rev=5 of how to do this and think that I can use it very easily. There is only one problem. I am having trouble trying to figure out how to add a link in the datatable. For example, if I take the existing examples json_template.txt :
[code]{
"sEcho": {{sEcho}},
"iTotalRecords": {{iTotalRecords}},
"iTotalDisplayRecords": {{iTotalDisplayRecords}},
"aaData":[
{% for country in querySet %}
[
"{{country.id}}",
"{{country.name}}",
"{{country.formal_name}}",
"{{country.capital}}",
"{{country.currency_code}}",
"{{country.currency_name}}",
"{{country.phone_prefix}}",
"{{country.tld}}"
]
{% if not forloop.last %}
,
{% endif %}
{% endfor %}
]
}
[/code]
Can I modify it such that one of the columns is a link, for example something like this:
[code]
{
"sEcho": {{sEcho}},
"iTotalRecords": {{iTotalRecords}},
"iTotalDisplayRecords": {{iTotalDisplayRecords}},
"aaData":[
{% for country in querySet %}
[
"{{country.id}}",
"{{country.name}}",
"{{country.formal_name}}",
"{{country.capital}}",
"{{country.currency_code}}",
"{{country.currency_name}}",
"{{country.phone_prefix}}",
"{{country.tld}}"
]
{% if not forloop.last %}
,
{% endif %}
{% endfor %}
]
}
[/code]
I am very new to all of this, so any help that I could get would be appreciated. I have been combing the forums and searching online and found several discussions that talk about doing something similar, but either I am not understanding them, or they are not working/not what I am looking for. Thank you very much for any help that you provide.
[code]{
"sEcho": {{sEcho}},
"iTotalRecords": {{iTotalRecords}},
"iTotalDisplayRecords": {{iTotalDisplayRecords}},
"aaData":[
{% for country in querySet %}
[
"{{country.id}}",
"{{country.name}}",
"{{country.formal_name}}",
"{{country.capital}}",
"{{country.currency_code}}",
"{{country.currency_name}}",
"{{country.phone_prefix}}",
"{{country.tld}}"
]
{% if not forloop.last %}
,
{% endif %}
{% endfor %}
]
}
[/code]
Can I modify it such that one of the columns is a link, for example something like this:
[code]
{
"sEcho": {{sEcho}},
"iTotalRecords": {{iTotalRecords}},
"iTotalDisplayRecords": {{iTotalDisplayRecords}},
"aaData":[
{% for country in querySet %}
[
"{{country.id}}",
"{{country.name}}",
"{{country.formal_name}}",
"{{country.capital}}",
"{{country.currency_code}}",
"{{country.currency_name}}",
"{{country.phone_prefix}}",
"{{country.tld}}"
]
{% if not forloop.last %}
,
{% endif %}
{% endfor %}
]
}
[/code]
I am very new to all of this, so any help that I could get would be appreciated. I have been combing the forums and searching online and found several discussions that talk about doing something similar, but either I am not understanding them, or they are not working/not what I am looking for. Thank you very much for any help that you provide.
This discussion has been closed.
Replies
One thing to be careful of, though, is making sure you don't prematurely end your string by trying to use the double quotes (or single quotes) in multiple contexts. I'm having trouble explaining this in plain English, so I'll show you what I mean
change line 9 to :
[code]
"{{country.id}}",
[/code]
or
[code]
'{{country.id}}',
[/code]
the good thing about both javascript and html is that they treat the single quote and double quote interchangably (you can use either - as long as your quotes match) so whichever you are using for your string in the JSON code, you can embed the other quote within it.