Thanks!!!

This helped a lot. Now it is working as I was hoping :)

drakko

trešdiena, 2013. gada 16. oktobris 16:14:39 UTC+2, ke1g rakstīja:
>
>
> On Wed, Oct 16, 2013 at 3:39 AM, drakko <[email protected] 
> <javascript:>>wrote:
>
>> ...
>>
>  
>
>> But accessing should_have_found_list in template has no problems. I have 
>> separate JS file that contains functions (event handlers for buttons etc.). 
>> I can't figure out (total newbie in JS :D ) how to access (or pass) 
>> should_have_found_list there. (Sorry I wasn't clear enough in my first post)
>>
>
> Things that the template context knows are available when the template is 
> rendered.  That is, when the template is turned into (in this case) an HTML 
> document (represented as a string).  This happens on the server.
>
> The only thing sent to the browser is that HTML document, notwithstanding 
> that that stuff in that HTML document may cause the browser to load other 
> stuff (like images, javascript files, and CSS files).
>
> But, specifically, the template context is not automatically included in 
> any way.
>
> If you have a template context variables that you wish to reference from 
> the javascript (which runs in the browser, long after template rendering is 
> complete) then you must arrange that the HTML document contain, within a 
> suitable script tag, javascript code that sets a javascript variable to a 
> javascript literal.  For example, if template context variables "count" and 
> "name" have, respectively, values 1 and "Joe", you might write something 
> like this in your template:
>
>     <script type="text/javascript">
>        var ct = {{ count }},
>             nm = "{{ name }}";
>     </script>
>
> Which becomes, in the HTML sent to the bowser:
>
>     <script type="text/javascript">
>        var ct = 1,
>             nm = "Joe";
>     </script>
>
> Now those values are available to the javascript running in the browser as 
> "ct" and "nm".  (You don't have to use different variable names.  I just 
> wanted it to be clear which were template context variables and which were 
> javascript variables).
>
> But note that not all python objects can be sent this way.  You can't, for 
> example, just send a queryset and expect to be able to use its "filter" 
> method from javascript.
>
> You have two choices for sending more complex objects who's ultimate parts 
> are representable as javascript scalars.  If they can be JSON encoded, then 
> that *IS* a javascript object literal.  Or you can iterate through the 
> object (and subobjects) rendering each by hand, including suitable 
> javascript object syntax separators and wrappers, like brackets, braces and 
> commas.
>
> Note, too, that my first code above does not work if the name variable 
> contains a double quote, since:
>
>     nm = "Joe "the schmoe" Gogo";
>
> isn't valid javascript.  The json built into modern pythons is willing to 
> encode a string as a suitable javascript object literal, with all necessary 
> escaping and with the quotes built in (even though this isn't legal JSON - 
> formally the top level object must be a javascript array or object).  So if 
> the name template context variable had been created in the view thusly:
>
>     ...
>     name = json.dumps(obj.name),
>     ...
>
> then the following is correct:
>
>     <script type="text/javascript">
>        var ct = {{ count }},
>             nm = {{ name }};
>     </script>
>
> Note that the quotes have been removed.
>
> [It might be useful to have a json dumps template filter.  Perhaps there's 
> one I haven't found.]
>  

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7ccd0e45-d4ec-4340-a6ea-ce88ff86c380%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to