Tuesday, October 15, 2013

Re: Django data to Javascript



On Tuesday, October 15, 2013 8:45:09 AM UTC-7, Larry....@gmail.com wrote:


You can embed js anywhere in your template, e.g.:

{% for shbf in should_have_found_list %}
    <script type="text/javascript">
        my_js_function(shbf);
    </script>
{% endfor %} 

I think you meant to write:

        my_js_function({{shbf}});

Another take on this, should you need it, is to send JSON directly over from the django view or model method into the template. Here's how I solved that recently:

        importance_options = json.dumps(dict(BundleNode.importance_options))

That will convert a Django queryset into a JSON object. Then, in your JS template, something like:

    var $options_obj = {{importance_options|safe}};

Season to taste.

./s

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e97192a-e293-4856-9c78-65e9133e30a4%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate