0

I'm developing a project that have not any slug field and this value create in template by:

{{ n.title|slugify }}

I have to use slug an jquery code, but variable aleays empty:

$("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?

Edit :

I have a queryset:

Model.objects.all()

How can I add manually field in this queryset?

TheNone
  • 5,684
  • 13
  • 57
  • 98

2 Answers2

0

No, you can't do it inside the javascript. Use this answer to slugify a text in the JS.

Community
  • 1
  • 1
catavaran
  • 44,703
  • 8
  • 98
  • 85
0

You can put the slug in the data attribute of your markup and then use javascript to grab it.

<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);
   });
Du D.
  • 5,062
  • 2
  • 29
  • 34