Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  19] [ 7]  / answers: 1 / hits: 38396  / 13 Years ago, sun, may 15, 2011, 12:00:00

Can I use Django's template tags inside Javascript? Like using {% form.as_p %} in jQuery to dynamically add forms to the page.


More From » jquery

 Answers
2

Yes, I do it frequently. Your javascript has to be served through django, but if you just have it in the html header as inline javascript you'll be fine.



E.g: I use this to put prefix on a dynamic formset I use.



{% extends base.html %}
{% block extrahead %}
<script type=text/javascript>
$(document).ready(function() {
{# Append fields for dynamic formset to work#}
{% for fset, cap, _, tid in study_formsets.fset_cap_tid %}
$(function() {
$('.form_container_{{ tid }}').formset({
prefix: '{{ fset.prefix }}',
formCssClass: '{{ tid }}',
extraClasses: ['myrow1', 'myrow2']
});
});
{% endfor %}
});
</script>
{% endblock %}


Note in base.html I have a html head where the jquery libraries are loaded, that contains {% block extrahead %}{% endblock %}.


[#92231] Friday, May 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ira

Total Points: 298
Total Questions: 112
Total Answers: 103

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;