简体   繁体   English

在javascript中使用django模板标签

[英]Using django template tag inside javascript

I'm developing a project that have not any slug field and this value create in template by: 我正在开发一个没有任何slug字段的项目,这个值在模板中创建:

{{ n.title|slugify }}

I have to use slug an jquery code, but variable aleays empty: 我必须使用slug和jquery代码,但变量为空:

$("select#model").click(function() {
      var u = $(this).find(":selected").val();
      var slg = $(this).find(":selected").text();
      var slug = "{{ slg|slugify }}";
      window.location.href("link that createt with u and slug);
   });

Is there anyway to using django template tag inside javascript? 无论如何在javascript中使用django模板标记?

Edit : 编辑:

I have a queryset: 我有一个查询集:

Model.objects.all()

How can I add manually field in this queryset? 如何在此查询集中手动添加字段?

No, you can't do it inside the javascript. 不,您不能在javascript中执行此操作。 Use this answer to slugify a text in the JS. 使用此答案来强制JS中的文本。

You can put the slug in the data attribute of your markup and then use javascript to grab it. 您可以将子弹放入标记的data属性中,然后使用javascript进行抓取。

<select id="model">
   <option data-slug="{{ slg|slugify }}" ... this is inside your template
</select>


$("select#model").click(function() {
      var $opt = $(this).find(":selected")
      var u = $opt.val();
      var slug = $opt.data("slug");

      window.location.href("link that createt with u and slug);
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM