Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我尝试使用.write()编辑文件时,它会添加很多空行_Python_Django - Fatal编程技术网

Python 当我尝试使用.write()编辑文件时,它会添加很多空行

Python 当我尝试使用.write()编辑文件时,它会添加很多空行,python,django,Python,Django,我正在尝试使用此代码更改文件的内容。当我编辑文件时,它可以工作,但它也会添加很多额外的空行 def edit(request,name): if request.method=="GET": content=util.get_entry(name) return render(request,"encyclopedia/edit.html",{ "content":cont

我正在尝试使用此代码更改文件的内容。当我编辑文件时,它可以工作,但它也会添加很多额外的空行

def edit(request,name):
    if request.method=="GET":
        content=util.get_entry(name)
        return render(request,"encyclopedia/edit.html",{
            "content":content,
            "name": name
        })
    else:
        entry = open(f"./entries/{name}.md","w+")
        entry.write(request.POST["content"])
        entry.close()
        return redirect(f"/wiki/{name}")
这是文件

# Python


Python is a programming language that can be used both for writing **command-line scripts** or building **web applications**.



cs50项目。我已经完成了。 下面的代码用于编辑功能

类入口表单(forms.Form):
title=forms.CharField(label=“条目标题”)
content=forms.CharField(widget=forms.Textarea(),label=“条目内容”)
def编辑(请求,标题=无):
如果request.method==“POST”:
表单=入口表单(request.POST)
如果不是标题:
如果form.is_有效():
title=表单。已清理的_数据['title']
内容=表单。已清理的_数据['content']
其他:
返回呈现(请求“encyclopedia/edit.html”{
“形式”:形式
})
elif title==form.data['title']:
title=form.data['title']
content=form.data['content']
util.save_条目(标题、内容)
返回HttpResponseRedirect(反向('wiki:greet',args=[title]))
如果不是标题:
返回呈现(请求“encyclopedia/edit.html”{
“表单”:EntryForm()
})
数据={'title':title'content':util.get_entry(title)}
填充的表格=入口表格(初始=数据)
返回呈现(请求“encyclopedia/edit.html”{
“头衔”:头衔,
“表格”:填写的表格,
“编辑”:真
})

cs50项目。我已经完成了。 下面的代码用于编辑功能

类入口表单(forms.Form):
title=forms.CharField(label=“条目标题”)
content=forms.CharField(widget=forms.Textarea(),label=“条目内容”)
def编辑(请求,标题=无):
如果request.method==“POST”:
表单=入口表单(request.POST)
如果不是标题:
如果form.is_有效():
title=表单。已清理的_数据['title']
内容=表单。已清理的_数据['content']
其他:
返回呈现(请求“encyclopedia/edit.html”{
“形式”:形式
})
elif title==form.data['title']:
title=form.data['title']
content=form.data['content']
util.save_条目(标题、内容)
返回HttpResponseRedirect(反向('wiki:greet',args=[title]))
如果不是标题:
返回呈现(请求“encyclopedia/edit.html”{
“表单”:EntryForm()
})
数据={'title':title'content':util.get_entry(title)}
填充的表格=入口表格(初始=数据)
返回呈现(请求“encyclopedia/edit.html”{
“头衔”:头衔,
“表格”:填写的表格,
“编辑”:真
})

您是否检查了请求。发布[“内容”]?最后可能有一些
\n
。我想使用
.strip()
可能会对您有所帮助。您能否显示
请求的真实内容。发布[“内容”]
?可能每行都表示为
\r\n
请求。POST[“内容”]
与我预期的一样,但我也尝试了
.strip()
,但仍然存在相同的问题。您是否检查了
请求。POST[“内容”]
?最后可能有一些
\n
。我想使用
.strip()
可能会对您有所帮助。您能否显示
请求的真实内容。发布[“内容”]
?可能每行都表示为
\r\n
请求。POST[“content”]
与我预期的一样,但我也尝试了
.strip()
,我仍然有相同的问题。谢谢。我只是忘记了util.save_entry()函数谢谢。我只是忘记了util.save_entry()函数