python - Django ContentFile() 意外空行 (django.core.files.base)

标签 python django

我对 django.core.files.base 的 ContentFile 函数有问题
我写了一个函数,允许我将文本保存到 .md 文件中

def save(title,content)
    filename = f"entries/{title}.md"
    if default_storage.exists(filename):
        default_storage.delete(filename)
    default_storage.save(filename, ContentFile(content))
我在表单中使用一个简单的 textarea,将内容提交到将保存它的路由。
问题是在保存每个新行后都会重复:
Hello
World
成为
Hello

World
而且这种情况一直在发生。如果你有 2 个空行,你最终会有 4 个......
难道我做错了什么?

最佳答案

我通过将 save_entry 函数的最后一行更改为

default_storage.save(filename, ContentFile(content.encode('ascii')))
根据这个答案 https://stackoverflow.com/a/4053205

关于python - Django ContentFile() 意外空行 (django.core.files.base),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62903909/

相关文章:

javascript - 在ajax中提交多部分表单?

Webfaction 上的 Django SSLMiddleware 问题

python - 如何更改 Qthread 内 Qtimer 的时间间隔?

Python 仅重单词和数字

python - 在 PyTorch 中使用 module.to() 移动成员张量

python 请求<响应[520]>

python - Django 不接受来自 python-requests.patch() 的数据

python - 在 enthought canopy 中突破 python shell 提示 "...:"

django - 在 Django 项目中设置 Sqlite PRAGMA 选项的地方

django - Django 中的存储后端是否处理删除旧的静态文件?