python - 如何使用 html 和 cheeypy 将对象放在隐藏的输入字段中

标签 python html cherrypy

    below is abc.html

    <div xmlns:xi="http://www.w3.org/2001/XInclude"
         xmlns:py="http://genshi.edgewall.org/" py:strip="True" >
            <div class="insync-bluthm-tbl-wrp">
                <div class="insync-bluthm-tbl-scroll">
                    <div class="insync-bluthm-tbl-scroll-inr">
                        <table class="insync-bluthm-tbl">
                        <thead>
                            <tr>
                                <th><div>File Name</div></th>
                            </tr>
                        </thead>
                        <tbody>
                            <input type="hidden" id="restorable_data" value="${restoreData}"/>
                            <tr>
                                <td><div>Dummy File name</div></td>
                            </tr>
                        </tbody>
                    </table>
                    </div>
                </div>
            </div>
    </div>

    below is python function

    @cherrypy.expose
    def can_restore_mds(self, *args, **kwargs):
    restoreData = {
            'abc': 'def',
            'akjshd': 'asd',
            'is_valid': 1,
    }
    restore_context = {
        'page': 'abc.html',
        'restoreData': restoreData,
    }
    html = render_page(restore_context, restore_context['page'])
    return {
        'html': html,
        'restoreData': restoreData,
    }
    return response

“restoreData”是变量,在服务器端渲染中没有以正确的格式注入(inject),任何人都可以帮助需要做什么吗?

最佳答案

您可以使用 json 转储函数来执行此操作:-

Python

# remember to import json in your python file
restoreData = {
    'abc': 'def',
    'akjshd': 'asd',
    'is_valid': 1,
}
restore_context = {
    'page': 'abc.html',
    'restoreData': json.dumps(restoreData),
}

HTML
<input type="hidden" id="restorable_data" value="${restoreData}"/>

关于python - 如何使用 html 和 cheeypy 将对象放在隐藏的输入字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085607/

相关文章:

c++ - Boost.Python 是如何工作的?

python - 在 Ubuntu 18.04 上使用 CUDA 和 FFMpeg 编译 OpenCV 时出错

python - 如何使用 networkx 和 python 将分层列表合并为一个,同时尊重每个列表的层次结构?

javascript - TinyMCE上传音频/视频/图像/文件

python - 如何关闭 cherrypy 服务器?

python - Daphne,安装 django channel 时出现扭曲的 pip 错误

javascript - 如何允许再次单击显示的 Bootstrap 选项卡?

html - 如何处理应该出现在另一个父级之上的子级的两个父级 div

python - Cherrypy 有什么好的 "contact us"食谱吗?

python - 为什么我在运行 CherryPy 教程之一时收到 ImportError