简体   繁体   中英

Inserting a JS variable within a Django template tag

This one is a bit tricky for me. I've thus-far resorted to query parameters instead a variable within the {% url %} tag, but I've just got to ask if it's doable:

I'd like to include a JS variable within my template tag. For example:

...
var foo = $(this).attr('title');
$('#bar').load("{% url app.views.view foo %}");
...

Can it be done?

Not doable. The HTML (and Javascript) are already rendered and served to the client by the time the Javascript is evaluated.

You need some other approach, like (as you mentioned) query parameters:

var foo = $(this).attr('title');
$('#bar').load("{% url app.views.view %}?foo=" + foo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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