Re: Javascript with built-in templates tags

2009-08-11 Thread Michael
On Tue, Aug 11, 2009 at 2:53 PM, WilsonOfCanada  wrote:

>
> I want to access the string from the list that is in the dictionary:
>
> function changeArea()
> {
>alert({{list_areas.British_Columbia}});
> }
>
> I get what I want in the generated HTML:
>
> function changeArea()
> {
>alert(['Metro Vancouver', 'Metro Vancouver A', 'Sunshine Coast']);
> }
>
> However, when I tried using:
>
> function changeArea()
> {
>alert({{list_areas.British_Columbia.0}});
> }
>
> I get this:
>
> function changeArea()
> {
>alert(Metro Vancouver);
> }
> (it is not a string)
>
> Thanks


Django template language is just a template that is all done on the server
side of things. Javascript is client side and doesn't care about the django
template. It thinks (rightly so) that this is just a static web page.

So when you use variables from a Django template, you have to expect all the
output to be written literally. So the proper way to do this with the
javascript you gave is by putting quotes around the variable:
alert('{{ Your Variable }}')

Now when you view source, that is what Django is sending to your browser and
your browser then executes the javascript.

I hope that helps,

Michael

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-11 Thread WilsonOfCanada

I want to access the string from the list that is in the dictionary:

function changeArea()
{
alert({{list_areas.British_Columbia}});
}

I get what I want in the generated HTML:

function changeArea()
{
alert(['Metro Vancouver', 'Metro Vancouver A', 'Sunshine Coast']);
}

However, when I tried using:

function changeArea()
{
alert({{list_areas.British_Columbia.0}});
}

I get this:

function changeArea()
{
alert(Metro Vancouver);
}
(it is not a string)

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-09 Thread esatterwh...@wi.rr.com



On Aug 7, 9:31 pm, WilsonOfCanada  wrote:
> Hellos,
>
> I was wondering how to use {{ }} variables in javascript functions.
>
> ex. onchange = "changeArea({{ mooman |safe|escapejs}});"
>
> Thanks

I had this problem too, it was pretty frustrating. I can to 2
different solutions.


1. new template in youapps directory calls whatever_js.html so you
know that is actually javascript.
* just write the 
 ...Code {{ Here }}
  
* and use the {% include  whatever_js.html %} tag where needed.

2. Probably not always the best option, but is useful for testing. put
the
 
  ... Code {{ Here }}
 
   in the template it self. This way django renders out all of the
code before the JavaScript is interpreted and runs as expected.

I'm pretty sure the Admin area does something pretty similar to the
whatever_js.html templates idea. If all else fails you could copy and
paste all of the js into your template the obtrusive way and see if it
works properly, then work backward by pulling bits out.

Maybe that helps?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-09 Thread Daniel Roseman

On Aug 9, 6:19 am, WilsonOfCanada  wrote:
> I tried that before, but it only seems to work when it is used on
> the .html file.
>
> onchange="changeArea('{{ list_areas.BC|safe|escapejs}}')
>
> I need it to be onchange="changeArea('{{ list_areas|safe|escapejs}}')
> so the function can use list_areas (My javascript and html are on
> separate files).
>
> On .js file,
>
> If I use:
> function changeArea(selectedAreas)
> {
>         alert({{list_areas.BC}});
>
> }
>
> with or without the |escapejs would cause the entire javascript not to
> work.
>
> If I use:
> function changeArea(selectedAreas)
> {
>         alert(selectedAreas.BC);
>
> }
>
> or
>
> function changeArea(selectedAreas)
> {
>         alert(selectedAreas["BC"]);}
>
> would be undefined.
>
> However, if it is:
>
> function changeArea(selectedAreas)
> {
>         alert(selectedAreas[0]);
>
> }
>
> I will get "{"
>
> Thanks again.  (If I missed the answer in the docs, sorry :) )


You are simply not thinking about this logically.

The 'html file' is a Django template. It is processed by Django's
templating engine, so template variables and tags are replaced
correctly, even within inline javascript.

The 'js file' is not a Django template. It is a static file -
presumably you are serving it via Apache or the development server's
static.serve view. Because it's not being processed by Django's
templating engine, template variables and tags are not replaced but
included as-is.

If you want to include Django variables in javascript, the way to do
it is as you did at first - have a small 

Re: Javascript with built-in templates tags

2009-08-08 Thread Sam Lai

Can you show us the generated template HTML, and if possible, the
expected result?

That way we can work out the issue with your django templates, rather
than trying to guess your code.

2009/8/9 WilsonOfCanada :
>
> I tried that before, but it only seems to work when it is used on
> the .html file.
>
> onchange="changeArea('{{ list_areas.BC|safe|escapejs}}')
>
> I need it to be onchange="changeArea('{{ list_areas|safe|escapejs}}')
> so the function can use list_areas (My javascript and html are on
> separate files).
>
> On .js file,
>
> If I use:
> function changeArea(selectedAreas)
> {
>        alert({{list_areas.BC}});
> }
>
> with or without the |escapejs would cause the entire javascript not to
> work.
>
> If I use:
> function changeArea(selectedAreas)
> {
>        alert(selectedAreas.BC);
> }
>
> or
>
> function changeArea(selectedAreas)
> {
>        alert(selectedAreas["BC"]);
> }
> would be undefined.
>
> However, if it is:
>
> function changeArea(selectedAreas)
> {
>        alert(selectedAreas[0]);
> }
>
> I will get "{"
>
> Thanks again.  (If I missed the answer in the docs, sorry :) )
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread WilsonOfCanada

I tried that before, but it only seems to work when it is used on
the .html file.

onchange="changeArea('{{ list_areas.BC|safe|escapejs}}')

I need it to be onchange="changeArea('{{ list_areas|safe|escapejs}}')
so the function can use list_areas (My javascript and html are on
separate files).

On .js file,

If I use:
function changeArea(selectedAreas)
{
alert({{list_areas.BC}});
}

with or without the |escapejs would cause the entire javascript not to
work.

If I use:
function changeArea(selectedAreas)
{
alert(selectedAreas.BC);
}

or

function changeArea(selectedAreas)
{
alert(selectedAreas["BC"]);
}
would be undefined.

However, if it is:

function changeArea(selectedAreas)
{
alert(selectedAreas[0]);
}

I will get "{"

Thanks again.  (If I missed the answer in the docs, sorry :) )
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread Luke Seelenbinder

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

That needs to be :
alert({{ selectedAreas.BC }});

Look at the documentation. This problem is fairly obvious if you do:
http://docs.djangoproject.com/en/dev/topics/templates/#variables

I'm not trying to be mean, but please look at the docs before you post
here, it wastes our time, and yours waiting for a response. Thanks!
We are more than happy to help, just please check the documentation first. ;)

Hope that works for you.

Luke Seelenbinder
luke.seelenbin...@gmail.com



On Sat, Aug 8, 2009 at 5:32 PM, WilsonOfCanada wrote:
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkp+Nf4ACgkQXQrGVCncjPy+0QCeJApu38xtnpmR9lEg4XgMrXaK
fQEAoIo0vbiYDVLkHLnTMZb5SlpnpELT
=QOCH
-END PGP SIGNATURE-

>
> I sent the variable as dictionary with lists
> {'BC:['Vancouver', 'Kamloops'], AB:['Calgary']}
>
> However, when I use the variable in the function, it is treated as a
> string.
>
> function changeArea(mooman)
> {
>        alert(selectedAreas["BC"]);
> }
>
> This was not defined.  Is there something more I need to add to the
> onchange="changeArea('{{ mooman|safe|escape}}');?
>
> Thanks
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread WilsonOfCanada

I sent the variable as dictionary with lists
{'BC:['Vancouver', 'Kamloops'], AB:['Calgary']}

However, when I use the variable in the function, it is treated as a
string.

function changeArea(mooman)
{
alert(selectedAreas["BC"]);
}

This was not defined.  Is there something more I need to add to the
onchange="changeArea('{{ mooman|safe|escape}}');?

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread Daniel Roseman

On Aug 8, 7:49 pm, WilsonOfCanada  wrote:
> Thanks,
> It worked, but out of curiosity, what do the additional quotes change
> (why are they needed)?

They're needed in Javascript, not in Django. If you looked at the
generated source (via View Source in your browser) you could have seen
that the outputted code looked like:
onchange = "changeArea(myvalue);">
whereas myvalue is a string, not a variable name, so needed to be put
in quotes.

--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread WilsonOfCanada

Thanks,
It worked, but out of curiosity, what do the additional quotes change
(why are they needed)?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread Matthias Kestenholz

On Sat, Aug 8, 2009 at 6:34 PM, WilsonOfCanada wrote:
>
> I am not sure; I am just trying to pass the variable from the
> dictionary using render_to_response to a javascript function.  The
> function is stored as a .js file.  This is a part of the code I am
> using it for:
>
> {{ selectCity|safe }}
>
>  onchange = "changeArea({{ mooman | safe | escapejs}});">
> 
>
> The {{ selectCity|safe }} works but the {{ mooman | safe | escapejs}}
> does not.  I was wondering if the syntax is incorrect or they cannot
> be used between " ".  Please show an example.
>

Why don't _you_ show us the generated HTML code and point out what's
unexpected about it? It will be much easier for us to help you.

A stab in the dark, since I have no idea what the content of mooman
might be: Maybe you want onchange="changeArea('{{ mooman|safe|escape
}}');" ? (Note the additional quotes around your {{ }} expression)


Matthias

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread WilsonOfCanada

No, I placed the spaces in so it is more readable (I tested with
spaces afterwards and they still do not work).  The function is passed
in by:


Re: Javascript with built-in templates tags

2009-08-08 Thread Luke Seelenbinder

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

I think your problem is just the spaces between the filters, try {{
mooman|safe|escapejs }}.
Other then that, as long as the mooman is set in the context... that
should work.

Luke

On Sat, Aug 8, 2009 at 12:34 PM, WilsonOfCanada wrote:
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkp9rFMACgkQXQrGVCncjPycTgCfeWdooEJqmAV2ngoI8sU8edKs
Cq0An0iGdBDQH1mY0O/LH7PgDK1YwWOc
=yqjZ
-END PGP SIGNATURE-

>
> I am not sure; I am just trying to pass the variable from the
> dictionary using render_to_response to a javascript function.  The
> function is stored as a .js file.  This is a part of the code I am
> using it for:
>
> {{ selectCity|safe }}
>
>  onchange = "changeArea({{ mooman | safe | escapejs}});">
> 
>
> The {{ selectCity|safe }} works but the {{ mooman | safe | escapejs}}
> does not.  I was wondering if the syntax is incorrect or they cannot
> be used between " ".  Please show an example.
>
> Thanks
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-08 Thread WilsonOfCanada

I am not sure; I am just trying to pass the variable from the
dictionary using render_to_response to a javascript function.  The
function is stored as a .js file.  This is a part of the code I am
using it for:

{{ selectCity|safe }}




The {{ selectCity|safe }} works but the {{ mooman | safe | escapejs}}
does not.  I was wondering if the syntax is incorrect or they cannot
be used between " ".  Please show an example.

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Javascript with built-in templates tags

2009-08-07 Thread Luke Seelenbinder

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Wilson,

Are you referring to rendering JS with template tags, or actual
template tags in JS?

In the first case, you use them like any other template tag.
The second case is only possible if there is some JS library that uses
Django-like templates.

Luke

On Fri, Aug 7, 2009 at 10:31 PM, WilsonOfCanada wrote:
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkp85c8ACgkQXQrGVCncjPyvtACfdp1MNDbfcA4FVXCEBA6vAlAs
NGgAnjlGepehXauPkVf20eW2wGtKiqfa
=29IC
-END PGP SIGNATURE-

>
> Hellos,
>
> I was wondering how to use {{ }} variables in javascript functions.
>
> ex. onchange = "changeArea({{ mooman |safe|escapejs}});"
>
> Thanks
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Javascript with built-in templates tags

2009-08-07 Thread WilsonOfCanada

Hellos,

I was wondering how to use {{ }} variables in javascript functions.

ex. onchange = "changeArea({{ mooman |safe|escapejs}});"

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---