Flask:在表单提交后重定向到同一页面 [英] Flask: redirect to same page after form submission

查看:914
本文介绍了Flask:在表单提交后重定向到同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模板中有两种形式:一种是发布内容,另一种是在服务器上激活文件删除功能:

 < div style =margin-bottom:150px;> 
< h4>删除< / h4>
< form method =postaction =/ delete>
< div class =form-group>
< input type =hiddenname =delete_input>< / input>
< / div>
< button type =submitclass =btn btn-dangerid =btnSignUp>删除< / button>
< / form>
< / div>

< div style =margin-bottom:150px;>
< h4>网址< / h4>
< form method =postaction =/>
< div class =form-group>
< textarea class =form-controlrows =5id =urlsname =url_area>< / textarea>
< / div>
< button type =submitclass =btn btn-primaryid =btnSignUp> Url< / button>
< / form>
< / div>

我的 app.py 看起来像这样:

  @ app.route(/)
def main():
return render_template(' index.html')


@ app.route('/',methods = ['POST'])
def parse_urls():
_urls = request split(\\\

image_list = get_images(_urls)
return render_template('index.html',images = image_list)


@ app.route('/ delete',methods = ['POST'])
def delete_images():
file_list = [f for os.listdir(./静态)if f.endswith(。png)]
for file_list:
os.remove(./ static /+ f)
image_list = []
conn = sqlite3.connect('_ db / database.db')

curs = conn.cursor()
sql =DROP TABLE IF EXISTS images
curs。执行(sql)
conn.commit()
conn.close()
返回render_template('index.html',images = image_list)

两个问题:


  • 我在提交表单后重新加载页面时收到表单重新提交消息

  • 我希望每件事都有一个url



需要这么使用重定向,以避免重复提交,并在调用删除后,我需要重定向到索引。



如何正确地做到这一点? b

我知道重定向 url_for ,但是如何重定向到同一页?

解决方案

你可以把所有的一个网址,但你必须检查哪个表单已提交。你可以有不同的视图,但不提供的东西,当提交删除表单时,将所有表单放在主页上只需添加删除视图函数到这个表单操作 action ={{url_for('delete_images')}}

  route'('/ delete',methods = ['POST'])
def delete_images():$ b $如果request.method =='POST':
#在这里做你的工作
return redirect(url_for('delete_images')


I have two forms on in my template: one, to post something and the second, to activate file deletion on the server:

<div style="margin-bottom:150px;">
    <h4>Delete</h4>
    <form method="post" action="/delete">
        <div class="form-group">
            <input type="hidden" name="delete_input"></input>
        </div>
        <button type="submit" class="btn btn-danger" id="btnSignUp">Delete</button>
    </form>
</div>

<div style="margin-bottom:150px;">
    <h4>URLs</h4>
    <form method="post" action="/">
        <div class="form-group">
            <textarea class="form-control" rows="5" id="urls" name="url_area"></textarea>
        </div>
        <button type="submit" class="btn btn-primary" id="btnSignUp">Urls</button>
    </form>
</div>

My app.py looks like this:

@app.route("/")
def main():
    return render_template('index.html')


@app.route('/', methods=['POST'])
def parse_urls():
    _urls = request.form['url_area'].split("\n")
    image_list = get_images(_urls)
    return render_template('index.html', images=image_list)


@app.route('/delete', methods=['POST'])
def delete_images():
    file_list = [f for f in os.listdir("./static") if f.endswith(".png")]
    for f in file_list:
        os.remove("./static/" + f)
    image_list = []
    conn = sqlite3.connect('_db/database.db')

    curs = conn.cursor()
    sql = "DROP TABLE IF EXISTS images"
    curs.execute(sql)
    conn.commit()
    conn.close()
    return render_template('index.html', images=image_list)

Two issues:

  • I get the form resubmission message when I reload the page after submitting the form
  • I would like to have one url for everything

The way I see it, I need so use redirects to avoid the duplicate submission and after calling delete, I need to redirect to index.

How can I do this correctly?

I know about redirect and url_for, but how do I redirect to the same page?

解决方案

You can put all in one url but you have to check which form has been submitted. You can have different views but don't render something, put all forms in home page when delete form has been submitted just add delete view function to this form action action="{{url_for('delete_images')}}"

@app.route('/delete', methods=['POST'])
def delete_images():
    if request.method == 'POST':
        # do your work here
        return redirect(url_for('delete_images')

这篇关于Flask:在表单提交后重定向到同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆