Thanks

Another problem is that, when I pass the
'variable_to_insert_into_template' to the jQuery function by using var
y = {{ variable_to_insert_into_template }}.

There is a error in fileBug, which is the invalid property id [Break
on this error] var y = {{ variable_to_insert_into_template }}\n

Regards
Min



On Jan 31, 8:48 am, Alex Robbins <alexander.j.robb...@gmail.com>
wrote:
> Yeah, that code would go in the views.py. simplejson is just an easy
> way to turn python variables into a string that javascript would be
> able to evaluate.
> Actually, if you are going to use the var x = make sure you take the
> "<script>" tags out of my example.
>
> I think your template should look like this:
> <script>
> var x = {{ variable_to_insert_into_template }};
> </script>
>
> Your views.py should look like this:
> tags = ['fun', 'exciting', 'cool']
> from django.utils import simplejson
> variable_to_insert_into_template = simplejson.dumps(tags)
>
> And make sure you "shove" variable_to_insert into the context like
> Malcolm explained. (Possibly using that js filter to make sure your js
> comes through ok.)
>
> On Jan 30, 3:29 pm, min <inuyasha...@gmail.com> wrote:
>
>
>
> > Thank you very much!
>
> > So you mean if I want pass the 'tags' list to the javascrip, the your
> > code example should be added in the views.py. After that, I can use
> > the list by using var x = {{variable_to_insert_into_template}} in my
> > javascript?
>
> > Regards
> > Min
>
> > On Jan 31, 3:48 am, Alex Robbins <alexander.j.robb...@gmail.com>
> > wrote:
>
> > > Sorry, a code sample would have been more helpful.
>
> > > With a python variable "tags" that has a list of tags to pass to js,
> > > it would look roughly like this:
>
> > > tags = ['fun', 'exciting', 'cool']
> > > from django.utils import simplejson
> > > variable_to_insert_into_template = "<script>%s</script>" %
> > > simplejson.dumps(tags)
>
> > > Pass variable_to_insert_into_template to the template and then just
> > > put {{variable_to_insert_into_template}} somewhere in your code.
>
> > > Just my 2 cents.
>
> > > On Jan 30, 10:42 am, Alex Robbins <alexander.j.robb...@gmail.com>
> > > wrote:
>
> > > > You might also consider using simplejson to dump the python variable
> > > > into something that JavaScript could understand. (This way you can
> > > > pass more complicated variables like an array or an object.) I
> > > > recently did that to get a list of tags out to some javascript on the
> > > > front end.
>
> > > > On Jan 29, 11:33 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
> > > > wrote:
>
> > > > > On Thu, 2009-01-29 at 21:11 -0800, min wrote:
> > > > > > Hi.
>
> > > > > > First, the code in the forms.py:
>
> > > > > > class TestForm(forms.Form):
> > > > > >     name = forms.CharField( max_length=30 )
>
> > > > > > Then, the variable is defined in the views.py:
>
> > > > > > def Test_page(request):
> > > > > >     form = TestForm()
> > > > > >     show_results = False
> > > > > >     variable = ''
> > > > > >     if request.GET.has_key('name'):
> > > > > >         show_results = True
> > > > > >         query = request.GET['name'].strip()
> > > > > >         variable  = 'custom value'
> > > > > >     variables = RequestContext(request, {
> > > > > >         'form': form,
> > > > > >         'show_results': show_results,
> > > > > >         'variable': variable
> > > > > >          })
> > > > > >     return render_to_response('Test.html', variables)
>
> > > > > > I know the value of variable can be accessed by using {{variable}} 
> > > > > > in
> > > > > > the Test.html. However, if I want use the jQuery in the Test.html 
> > > > > > and
> > > > > > pass the value of this variable to the jQuery function, how to do
> > > > > > that?
>
> > > > > > I have tried: var x = $("variable").val(), and not succeed.
>
> > > > > The context dictionary you pass to render_to_response is used by
> > > > > render_to_response to produce a string. That string is what is sent 
> > > > > back
> > > > > to the browsers. The variables (parameters, whatever we want to call
> > > > > them) are shoved into the template -- in a nice way; they're made very
> > > > > comfortable -- and become static data in the final result. That is, 
> > > > > they
> > > > > are *not* variables from the browser's perspective. Template rendering
> > > > > happens fully on the server side.
>
> > > > > If you want to access this data via Javascript, you first have to make
> > > > > it available as a Javascript variable in the template. For example, 
> > > > > you
> > > > > could write this in your template (inside a script block).
>
> > > > >         var x = "{{ variable|escape_js }}";
>
> > > > > The {{...}} bit is converted to a string by the (server-side) template
> > > > > rendering. Then "x" is available to the browser-side Javascript. Clear
> > > > > as mud?
>
> > > > > Regards,
> > > > > Malcolm- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to