【问题标题】:Using Python and Bottle to edit text data in a dictionary -- how to put the original dictionary data into the textarea?使用 Python 和 Bottle 编辑字典中的文本数据——如何将原始字典数据放入 textarea?
【发布时间】:2014-02-26 07:13:08
【问题描述】:

使用 textarea 时,很容易将固定的起始字符串放入 HTML 表单中,例如:

<form action="/whatever" method="POST">
<textarea cols="60" rows="10" name="whatever" maxlength="2500">dddddd</textarea>
</form>'''

用户在表单中看到数据 dddddd 并可以在那里进行编辑。

但是我怎样才能将可变数据放入表单中——字典中的原始数据,以便用户可以从它开始并进行更改?如果在 Python 中我有一个变量 y = 'xxx' 并将 y 放入表单中,那么用户将看到变量名 y 而不是它包含的文本。

当然我可以打印数据并让用户在编辑之前将其剪切并粘贴到表单中,但肯定有比这更好的东西。我宁愿不需要 Javascript,也不需要 HTML5。

【问题讨论】:

  • 您是否使用模板生成 HTML?如果是这样,您应该能够使用模板将 y 的值放入任何其他模板值中。

标签: python html forms textarea bottle


【解决方案1】:

你需要在变量周围加上花括号:

<form action="/whatever" method="POST">
<textarea cols="60" rows="10" name="whatever" maxlength="2500">{{y}}</textarea>
</form>

查看Bottle's SimpleTemplate Engine documentation 中的基本 API 用法。

【讨论】:

  • 谢谢!我以为会有一些简单的事情。
  • @user3354588 太好了,很高兴我能帮上忙。如果此答案解决了您的问题,请将其标记为正确答案,以便关闭您的问题。
【解决方案2】:
# To help beginners, here is a minimal Python with Bottle script that runs.
# Python 3 with Bottle: How to display an HTML form pre-populated with variable data.

import bottle
from bottle import template, run
from bottle import get, post

@get('/')
def yourtest():
    x = "xxx"
    return template (ADMIN_FORM, y = x)

ADMIN_FORM = '''
    <form action="whatever" method="POST">
    <textarea cols="60" rows="10" name="whatever" maxlength="2500">{{y}}</textarea>
    </form>'''

run(host='localhost', port=8080)   # Comment out for remote server.
# And run the remote server here

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2022-12-05
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多