Brian D
Brian D

Reputation: 10133

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?

Upvotes: 5

Views: 3212

Answers (1)

tcarobruce
tcarobruce

Reputation: 3838

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);

Upvotes: 6

Related Questions