Django自定义模板标签在javascript [英] Django custom template tags in javascript

查看:107
本文介绍了Django自定义模板标签在javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义模板标签,如果作为学生的参数ID传递,则返回假设学生姓名和卷号。

  @ register.inclusion_tag('snippet / student_name.html')
def st_name_tag(profile,disp_roll = disp_name = True):
#some计算
返回{'full_name':student.name,
'roll':student.roll_number,
}

模板(包括)由一行Html文件组成,用一行写(避免 unterminated string我只想从里面调用 st_name_tag

JS函数



我的JS看起来像:

  {% load profile_tag%} 
< script type =text / javascript>
eventclick:function(st){
var div =('< div>< / div>');
var st_id = st.id;
if(st.status =='pass'){
div.append('< p>学生名称:{%st_name_tag'+ st_id +'%}< / p>');
}
}

到目前为止,我尝试了上述方法,同时删除 + ''标志从 st_id varaible。这没有帮助我。帮助请!

解决方案

您正在尝试根据用户的交互渲染模板。第一个发生在服务器(服务器端,通常被称为),后者发生在用户的浏览器上。



这些发生的顺序首先是在服务器上呈现模板,在浏览器中发送和显示,然后用户与js进行交互。因为这个事实,正如我在评论中提到的,不可能影响在javascript中呈现的模板。



我建议您使用 jax ,以实现此目的。无论何时发生迭代,您将异步地向服务器发出请求,向您呈现新数据。


I have a custom template tag that returns suppose name of a student and roll number if passed as an argument id of the student.

@register.inclusion_tag('snippet/student_name.html')
def st_name_tag(profile, disp_roll=True, disp_name=True):
    #some calculations
    return {'full_name':student.name,
            'roll':student.roll_number,
           }

The template(included) consists of some Html file which is written in a single line(to avoid unterminated string literal error from js).

I simply want to call the st_name_tag from inside the JS function.

My JS looks like:

{% load profile_tag %}
<script type = "text/javascript">   
eventclick : function(st){
     var div = ('<div></div>');
     var st_id = st.id;
     if (st.status == 'pass'){
          div.append('<p>Student Name:{% st_name_tag '+st_id+' %}</p>');
     }
}

So far I tried the above method along with removing the + and '' signs from st_id varaible. That hasnt helped me at all. Help Please!

解决方案

You are trying to render a template based on the interaction by user. The first happens on the server (server-side as it is often referred to), and the latter happens on the user's browser.

The order that these happen is first to render the template on server, send and present in browser, then user interacts with js. Because of this fact, as I mentioned in the comment, it is not possible to affect the template rendered within javascript.

I would recommend you to use ajax in order to accomplish this. Whenever an iteraction occurs, you asynchronously make a request to the server to present you with new data.

这篇关于Django自定义模板标签在javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆