【发布时间】:2017-11-13 08:38:26
【问题描述】:
到目前为止我有这个:
@app.route('/view/<postname>')
def view_post(postname):
posts_folder = os.path.abspath(os.path.join(app.root_path, 'content', 'posts'))
filename = safe_join(posts_folder, postname)
with open(filename, 'rb') as f:
content = f.read()
html = markdown.markdown(content)
return render_template("view.html",html=html,postname=postname)
class PostForm(FlaskForm):
postTitle = StringField('postTitle', validators=[DataRequired()])
postText = TextAreaField('postText',validators=[DataRequired()])
@app.route('/submit', methods=('GET', 'POST'))
def submit():
postname = request.form["postTitle"]
print postname
posts_folder = os.path.abspath(os.path.join(app.root_path, 'content', 'posts'))
filename = safe_join(posts_folder, postname)
with open(filename,'wb') as f:
f.write(request.form['postText'])
while True:
if os.path.exists(filename):
return redirect(url_for(view_post(postname)))
break
else:
pass
如您所见,我有一个表单,当它提交时,它指向我的 /submit 路由。此路由创建一个新的帖子文件并将帖子的内容写入其中。然后,它应该重定向到查看帖子路由,以便它可以看到最近创建的帖子。在尝试加载此路由之前,它需要等到帖子完成对文件的写入。你可以看到我试图在 while True 循环中处理这个问题。但是,现在错误显示:
BuildError: Could not build url for endpoint u'<!DOCTYPE html>\n<html>\n<p>b</p>\n<br>\n<a href="/edit/a">\n a\n </a>\n\n<html>'. Did you mean 'edit_post' instead?
好像 url_for(view_post(postname)) 以某种方式试图查看原始 html。但是,当我打印 postname 时,它会打印 postTitle 对象的内容,这是我打算保存并重新路由到的文件名。
【问题讨论】: