17

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?

user3629892
  • 2,960
  • 9
  • 33
  • 64

5 Answers5

21

You can get the currently requested URL by request.url:

So, to redirect to the same page use:

redirect(request.url)
Mahmoud
  • 2,683
  • 1
  • 30
  • 32
  • does not work if there is anything quoted in the url E.g. if you request http://example.org/?whatever=bla%20bla request.url will contain "http://example.org/?whatever=bla bla" (blank unquoted) wihch makes the redirection fail. AFAIK, there is no way to quote reliably to the original again – paul Jan 20 '20 at 16:26
18

This worked perfectly for me, in last line:

return redirect(request.referrer)
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
16

Change form action to action="{{url_for('delete_images')}}". And for redirection you can use code below:

@app.route('/delete', methods=['POST'])
def delete_images():
    return redirect(url_for('delete_images'))
metmirr
  • 4,234
  • 2
  • 21
  • 34
  • 1
    Thanks. For this specific example, you don't really need to check `request.method` is equal to POST here cause the app route would do that for you already. – chasethesunnn Jun 15 '21 at 00:17
16

As archer said below:

return redirect(request.referrer)

This is useful when you have a button that uses a route to perform a given function when it is clicked - you don't want to return the user to the URL for that button - you want to return the user to the URL that the button route was referred by, i.e. the page the user was on when they clicked the button.

However, as Mahmoud said:

redirect(request.url)

This is perfect if you perform a function on a page that doesn't use routes or special URLs or anything like that. It essentially just refreshes the page.

Alex Chashin
  • 3,129
  • 1
  • 13
  • 35
Jessie Danquah
  • 161
  • 1
  • 3
  • ```return redirect(request.referrer)``` gave me a "Method Not Allowed The method is not allowed for the requested URL." error. But the redirect(request.referrer) worked. – G_L Jun 26 '22 at 17:45
0

One way is set the current url by Javascript in your form. like:

<form method="post" action="/delete">
    <div class="form-group">
        <input type="hidden" name="delete_input"></input>
    </div>
    <input type=hidden class=current_url value="" name=current_url>
    <button type="submit" class="btn btn-danger" id="btnSignUp">Delete</button>
</form>

And set the hidden input value by JS, like,

function get_current_url() {
    var url=window.location.href;
    return url
}

$(window).on('load',function(){
    var current_url=document.getElementsByClassName('current_url')[0];
    current_url.value=get_current_url();

})

At server, redirect to the url that post data

tim
  • 1,454
  • 1
  • 25
  • 45