javascript variable in url resolver template tag

2,435 views
Skip to first unread message

Sven Richter

unread,
Aug 9, 2009, 11:43:16 AM8/9/09
to django...@googlegroups.com
Hi all,

i wanted to know if it is possible to pass a Javascript variable to
the url template tag?

Like:
<html>
...
<script language="javascript">
var param = 12;
var url = {% url url_name param %}
...

I've done a lot af googling but havent found anything.


Greetings
Sven

Matthias Kestenholz

unread,
Aug 9, 2009, 11:52:09 AM8/9/09
to django...@googlegroups.com

It's not possible like this, of course. The template tags are
processed at the server, the javascript later at the client. What is
it you'd like to do? It's hard to help out if you don't tell us what
you're trying to achieve.


Matthias

Daniel Roseman

unread,
Aug 9, 2009, 11:57:29 AM8/9/09
to Django users
How could this work? The template tag is resolved server-side when the
template is output, whereas the javascript runs client-side later.

The best you could do would be to parse the output of the tag and
replace it dynamically with the javascript param, but since to do that
you'd have to know the format of the url, it sort of removes the point
of using the tag in the first place.
--
DR.

Sven Richter

unread,
Aug 9, 2009, 11:59:51 AM8/9/09
to django...@googlegroups.com
I am looking for a rating function AJAX style.
So what i am trying to do is the following:

Click on a link <a id="vote_up"...
register a Jquery function to call on that click with:
$('a#vote_pos').click(rate_it);
and then in rate_it call the url rate with two paramaters int entry and string pos_neg
like
$.ajax( {type:"POST", url:/entry/rate/pos_neg, ...
and i wanted to build the url with the url tag like:
url:{% url rate entry pos_neg %}

Sven

Sven Richter

unread,
Aug 9, 2009, 12:02:17 PM8/9/09
to django...@googlegroups.com
Ok, i think i understand the problem now.
So i have to hardcode the url in my javascript function?
That was what i wanted to circumvent.

Sven

Daniel Roseman

unread,
Aug 9, 2009, 2:20:45 PM8/9/09
to Django users
On Aug 9, 4:59 pm, Sven Richter <sver...@googlemail.com> wrote:
> I am looking for a rating function AJAX style.
> So what i am trying to do is the following:
>
> Click on a link <a id="vote_up"...
> register a Jquery function to call on that click with:
> $('a#vote_pos').click(rate_it);
> and then in rate_it call the url rate with two paramaters int entry and
> string pos_neg
> like
> $.ajax( {type:"POST", url:/entry/rate/pos_neg, ...
> and i wanted to build the url with the url tag like:
> url:{% url rate entry pos_neg %}
>
> Sven
>

Rather than having a dynamic URL with parameters, you'll just have to
make the parameters part of the POST. So you can still use the url tag
at render time to output the URL, but instead of it being /entry/5/
rate/positive/ it should just be eg /entry/rate/, and the values for
rate and pos_neg are keys in the POST.
--
DR.

Sven Richter

unread,
Aug 10, 2009, 3:01:59 AM8/10/09
to django...@googlegroups.com
Ok, thank you very much, thats how i am gonna do it.
I just didnt think about the fact that django is being done on
the server side and the script on the client side.


Greetings
Sven

Jani Tiainen

unread,
Aug 10, 2009, 3:35:49 AM8/10/09
to django...@googlegroups.com
Sven Richter kirjoitti:

> Ok, i think i understand the problem now.
> So i have to hardcode the url in my javascript function?
> That was what i wanted to circumvent.

No you don't have to.

There is two ways, either you just use base URL like {% url url_name %}
and send parameters as POST (or well, you can do GET params if you like
to) or construct rest of the URL in JS.

Personally I'm always doing it with POST parameters in Ajax. It's just
simpler that way.

One reason not to use hardcoded URL's is avoid modifying scripts when
deploying. Specially when you start deploying same project many times to
suburls it starts to come pretty valueble to have template resolving URL.

> On Sun, Aug 9, 2009 at 5:57 PM, Daniel Roseman <dan...@roseman.org.uk
> <mailto:dan...@roseman.org.uk>> wrote:
>
>
> On Aug 9, 4:43 pm, Sven Richter <sver...@googlemail.com

> <mailto:sver...@googlemail.com>> wrote:
> > Hi all,
> >
> > i wanted to know if it is possible to pass a Javascript variable to
> > the url template tag?
> >
> > Like:
> > <html>
> > ...
> > <script language="javascript">
> > var param = 12;
> > var url = {% url url_name param %}
> > ...
> >
> > I've done a lot af googling but havent found anything.
> >
> > Greetings
> > Sven
>
> How could this work? The template tag is resolved server-side when the
> template is output, whereas the javascript runs client-side later.
>
> The best you could do would be to parse the output of the tag and
> replace it dynamically with the javascript param, but since to do that
> you'd have to know the format of the url, it sort of removes the point
> of using the tag in the first place.
> --
> DR.

--
Jani Tiainen

Reply all
Reply to author
Forward
0 new messages