Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this in my view:

string_location = myaddress2
    geodata = []
    for place, (lat, lng) in g.geocode(string_location,exactly_one=False):
        geodata.append((place, (lat, lng)))

    geodata_results = len(geodata)

    data = {"geodata": geodata, "geodata_results":geodata_results }
    return render_to_response("business/business_view.html",
                              data, context_instance=RequestContext(request))

How would I "handle" / convert geodata into JSON and pass it to my template so that I can "loop" through it like an array?

Am I right to think that I can do it this way? If not, then please suggest on a better solution.

Thanks!

UPDATE

var geodata = "[["M. L. Quezon Street<br/>Mandaue City, Philippines", [10.351381999999999, 123.923535]], ["Talamban<br/>Cebu City, Philippines", [10.353527, 123.91352500000001]]]"; 

I think the JSON is not escaped? How do I escape special characters inside the json string? I keep getting a newline error.

For PHP, I would json_encode() to fix this. Like in this post: http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines BUT how do I do that in Python/Django?

share|improve this question

2 Answers

up vote 5 down vote accepted

Okay, I solved my problem and would like to answer my own question. I figured it would be better for the other users here.

First, get the file here: http://www.JSON.org/json_parse.js

var geodata = json_parse("{{geodata|escapejs}}");

I just used escapejs: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#escapejs

EDIT: Thanks to Ignacio Vazquez-Abrams. It was him that helped me in #python Freenode. Should have credited him when I made this post. I didn't know he was in Stackoverflow.

share|improve this answer
1  
Oh did you then. – Ignacio Vazquez-Abrams Jul 28 '10 at 2:03
Seriously thanks ;) I didn't know you were in Stackoverflow – wenbert Jul 28 '10 at 10:29
how is this different from using var geodata = eval("{{geodata|escapejs}}"); ? – simon Mar 30 '11 at 7:41
1  
it is not safe to use eval() – wenbert Mar 30 '11 at 14:25
1  
How is this different from var geodata = {{ geodata|escapejs }} (not in a string) ? – Izkata Feb 19 at 17:21

You could use the built-in json module:

>>> import json
>>> geodata = [ ( "Here", (1003,3004) ), ("There", (1.2,1.3)) ]
>>> json.dumps(geodata)
'[["Here", [1003, 3004]], ["There", [1.2, 1.3]]]'

You can then simply embed the resulting string inside a javascript script:

<script type='text/javascript'>
var geodata = {{ geodata|safe }};
</script>
share|improve this answer
I get an error after this line: var geodata = "[[&quot;M. L. Quezon Street&lt;br/&gt;Mandaue City, Philippines&quot;, [10.351381999999999, 123.923535]], [&quot;Talamban&lt;br/&gt;Cebu City, Philippines&quot;, [10.353527, 123.91352500000001]]]"; I think the JSON is not escaped? How do I escape special characters inside the json string? – wenbert Jul 27 '10 at 16:24
Could it be that you added to your template var geodata = "{{ geodata }}";? It should be without the quotes. – adamk Jul 27 '10 at 17:51
Nope. I have tried that without the quotes. I get a "Uncaught SyntaxError: Unexpected token &" in Google and this error: "var geodata = [[&quot;M. L...;, [10.353527, 123.91352500000001]]];\n" in Firefox – wenbert Jul 27 '10 at 23:26
2  
you must have autoescaping on... try to put in your template {{ geodata|safe }} (also fixed in the answer) – adamk Jul 28 '10 at 6:42
Why should it be without the quotes? Strings evaluated in Django templates don't have quotes. – Stavros Korokithakis Jun 26 '12 at 13:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.