【问题标题】:python django newline not working with replace in richtextfieldpython django换行不能在richtextfield中使用replace
【发布时间】:2021-12-08 03:28:36
【问题描述】:

models.py

description = RichTextField(blank=True, null=True)

admin.py

description = "*text1/*text2/*text3"




desired_output = "*text1
                  *text2
                  *text3"

#我尝试使用替换,但它不起作用 - description.replace("/", "\n")

#replace 无法在 RichTextField 中获取换行符 任何解决方案?

【问题讨论】:

    标签: python python-3.x django django-models


    【解决方案1】:

    使用\n 未能在richtextfield 中生成新行。将\n换成html样式标签换行<br>解决了这个问题。

    description = "*text1/*text2/*text3"
    
    desired_description = re.sub(r"/", "<br>", description)
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多