开发者

how to access python object in javascript function?

开发者 https://www.devze.com 2023-02-17 06:59 出处:网络
that means now i am working on python with cherryypy and genshi framework with help of this i send python variable to html file using genshi but i want to access variable that variable in javascript f

that means now i am working on python with cherryypy and genshi framework with help of this i send python variable to html file using genshi but i want to access variable that variable in javascript function so how can i do?..

html file is:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/">
<head>
    <title>Home</title>
    <script type="text/javascript">
        function setparent(val) {
            document.getElementById("tpar").value=val;
        }
    </script>
</head>
<body>
    <div >
        <table>
            <div py:for="user in users">
                <tr>
                    <td>
                        <div titl开发者_如何学编程e="${user.userid}" onclick="setparent('${user.userid}')">${user.fname+" "+ user.lname}</div>
                        <py:if test="user.resource.__len__() > 0">
                            <div>
                                <py:for each="res in user.resource">
                                    <div><a href="">${user.resource[res]}</a></div>
                                </py:for>
                            </div>
                        </py:if>
                    </td>
                </tr>
            </div>
        </table>
    </div>
</body>
</html>

as i want to access "users" variable of py into javascriptfunction

can u tell me this?


The proper way to pass values from python to javascript is - especially if they can contain more than just plain numbers or a very restricted charset (basically [a-zA-Z0-9] and non-quoting symbols) - running the value through a json encoder (json.dumps(...)) to ensure they do not break anything.

0

精彩评论

暂无评论...
验证码 换一张
取 消