在jQuery / Javascript中使用Django模板标签? [英] Use Django template tags in jQuery/Javascript?
本文介绍了在jQuery / Javascript中使用Django模板标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{%form.as_p%}
来动态添加表单到页面。 解决方案
是的,我经常做。你的javascript必须通过django来提供,但是如果你只是把它放在HTML标头里面,就像在线javascript一样,你会很好的。
例如:我用这个来放我使用的动态表单上的前缀。
{%extendsbase.html%}
{%block extrahead %}
< script type =text / javascript>
$(document).ready(function(){
{#为动态表单添加字段工作#}
{%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%}
注意在base.html我有一个html head
其中加载了jquery库,其中包含 {%block extrahead%} {%endblock%}
。
Can I use Django's template tags inside Javascript? Like using {% form.as_p %}
in jQuery to dynamically add forms to the page.
解决方案
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 %}
.
这篇关于在jQuery / Javascript中使用Django模板标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文