【问题标题】:How to store multiline text in html textarea from python?如何在python的html textarea中存储多行文本?
【发布时间】:2013-12-16 21:57:46
【问题描述】:

我有一个 sql 数据库,其中每一行都包含文本段落。我想在它自己的 html textarea 中显示每一行文本。我正在使用python生成html页面。我尝试了以下方法,但 textarea 的正文始终为空。有谁知道我可以做些什么来解决这个问题?

# didn't work
# dict = {'example':results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), 'arg':results[i]["GImageID"]}
# s = "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe1 - {example}</textarea>".format(**dict)

# gives an error on the d even though a tutorial said I should do it this way
# s = "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg:d}\">maybe2 - {example:d}</textarea>".format(**dict)

#extra += s

#------------------------------------------------------
teaser = results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;")


# didn't work - empty
# extra += "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe3 - {example}</textarea>".format(example=teaser, arg=results[i]["GImageID"])

# didn't work - empty
# extra += "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe3.5 - {example}</textarea>".format(example=results[i]["GImageTeaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), arg=results[i]["GImageID"])

# didn't work - empty
# t = Template("<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra$arg\">maybe4 - $example</textarea>")
# s = t.substitute({ 'example': results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), 'arg': results[i]["GImageID"]})
# extra += s

# didn't work - empty
# html = Template("<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra$arg\">maybe5 - $example</textarea>")
# result = html.safe_substitute(example=teaser,arg='Vishnu')

# result = html.safe_substitute(example=results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"),arg='Vishnu')
# extra += result

# didn't work - empty
# extra += """<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra%d\">maybe6 - %s</textarea>""" % (results[i]["GImageID"], results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"))

上面的文字在for循环中

for i in range(len(results)):

我已经输出了文本 results[i]["Teaser"] 来验证它不是空的。 GImageID 打印得很好,但包含段落的 Teaser 文本始终为空。

如果您需要任何其他信息,请告诉我。我已经被困了几个小时,我找不到任何可行的解决方案。谢谢!

【问题讨论】:

  • 您是否尝试过手写textarea 看看是否有效? How can I “pre-fill” the value of a textarea in an HTML form?
  • 是的,我去 w3schools.com 做了一个文本区域,并用输出填充了文本的正文。

标签: python html


【解决方案1】:
import cgi

text = results[i]["Teaser"] # the text that you want to put into textarea
html_safe_text = cgi.escape(text) # < > & -> &lt; &gt; &amp;
textarea_html = u"<textarea>{}</textarea>".format(html_safe_text)

您可以在浏览器中打开它进行调试:

import tempfile
import webbrowser
import time

def open_in_browser(html):
    """like lxml.html.open_in_browser() but `html` is a bytestring."""
    with tempfile.NamedTemporaryFile("wb", 0, suffix='.html') as file:
        file.write(html)
        webbrowser.open(file.name)
        time.sleep(60) # give the browser a minute to open before
                       # deleting the file

open_in_browser((u'''<!doctype html>
    <meta charset="utf-8">
    <title>Fill textarea</title>
''' + textarea_html).encode('utf-8'))

这是一个独立的fill-textarea.py script,你可以试试。

【讨论】:

  • 我修改了一行代码 textarea_html = u"".format(html_safe_text) 因为我使用的是 2.6 但正在生成的文本区域仍然是空的.有没有办法做到这一点?
  • @user2492566:这意味着错误在不同的地方,例如,len(results) 可能为零。
  • @user2492566:我添加了complete code example. Try it.
  • 感谢您的示例。您之前的代码是正确的。我只是将错误版本的代码上传到服务器。你刚刚为我节省了很多时间。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 2021-04-11
  • 2015-11-02
  • 2021-11-12
  • 1970-01-01
相关资源
最近更新 更多