Thanks a lot. Besides, what's the meaning for the 'escape_js' in [var x = "{{ variable|escape_js }}"] ?
Regards Min On Jan 30, 4: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 - --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---