我对 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/