【问题标题】:Google App Engine Datastore multiline entries not displayed as multiline in HTMLGoogle App Engine Datastore 多行条目未在 HTML 中显示为多行
【发布时间】:2011-04-12 09:45:51
【问题描述】:

以 Google App Store 留言簿演示为例,在多行输入条目并将其存储时,当回读并显示时,它会显示在一行中。
我们怎样才能让它看起来与最初输入的完全一样,多行?

数据库模型是这样的:

class Greeting(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)

而提交表单是这样的:

self.response.out.write("""
          <form action="/sign" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
      </html>""")

【问题讨论】:

    标签: python google-app-engine multiline


    【解决方案1】:

    Html 会忽略 EOL 特殊字符,例如 \r\n\n

    这里有一些选项:

    1. 用合适的html&lt;br&gt;标签替换特殊字符

    2. 将多行文本包装在&lt;pre&gt; 标签内

    3. 如果您使用 webapp 模板,请按照 @wooble 的建议尝试使用 {{greeting.content|linebreaks}}

    4. 按照 @Nick 的建议在 CSS 中设置 white-space:pre(例如 here

    【讨论】:

    • 如果你使用模板,(1)可能就像在模板中添加过滤器一样简单;在 django(这是 webapp 模板使用的)中,执行{{greeting.content|linebreaks}}
    • 3) 在 CSS 上为您正在使用的元素设置“空白:pre”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多