Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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 Flask:提交后如何重定向到post视图?_Python_Flask - Fatal编程技术网

Python Flask:提交后如何重定向到post视图?

Python Flask:提交后如何重定向到post视图?,python,flask,Python,Flask,到目前为止,我有: @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:

到目前为止,我有:

@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
@app.route(“/view/”)
def view_post(postname):
posts\u folder=os.path.abspath(os.path.join(app.root\u path,'content','posts'))
filename=safe\u join(posts\u文件夹,postname)
将open(filename,'rb')作为f:
content=f.read()
html=标记。标记(内容)
返回render_模板(“view.html”,html=html,postname=postname)
班级PostForm(烧瓶表格):
postTitle=StringField('postTitle',验证器=[DataRequired()]))
postText=TextAreaField('postText',验证器=[DataRequired()]))
@app.route('/submit',methods=('GET',POST'))
def submit():
postname=request.form[“postTitle”]
打印邮名
posts\u folder=os.path.abspath(os.path.join(app.root\u path,'content','posts'))
filename=safe\u join(posts\u文件夹,postname)
将open(filename,'wb')作为f:
f、 写入(请求.表单['postText'])
尽管如此:
如果os.path.存在(文件名):
返回重定向(url\u for(view\u post(postname)))
打破
其他:
通过
如您所见,我有一个表单,当它提交时,它将指向我的/submit路径。此路由将创建一个新的post文件,并将post的内容写入其中。然后,它应该重定向到view post路由,以便可以看到最近创建的帖子。在尝试加载此路由之前,它需要等待post完成对文件的写入。您可以看到,我试图在while-True循环中处理这个问题。但是,现在错误显示:

BuildError: Could not build url for endpoint u'<!DOCTYPE html>\n<html>\n&lt;p&gt;b&lt;/p&gt;\n<br>\n<a href="/edit/a">\n        a\n    </a>\n\n<html>'. Did you mean 'edit_post' instead?
BuildError:无法为端点u'\n\npb/p\n
\n\n\n'生成url。你是说“编辑帖子”吗?

就好像(view_post(postname))的url_试图查看原始html一样。但是,当我打印postname时,它会打印postTitle对象的内容,这是我要保存并重新路由到的文件名

我最后只是做了:

@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'])
    return redirect(url_for('view_post', postname=postname))
真管用!问题是它不会等到文件加载,这对于真正大的文件来说可能是个问题。但看起来它们必须相当大。尽管我会把这个问题留到别人有实际答案的时候(假设上传文件确实需要花费大量的时间)。此外,我的错误也发生了,因为我忘记了url\u底部重定向的“查看\u帖子”周围的引号