0

I am running a flask application to save details in dynamodb. The issue is that when I click the refresh button after submitting a data, same data is again resubmitting. How can I prevent this resubmission on clicking the refresh button

Code

@app.route('/',methods = ['GET','POST'])
def parseurl():
     if request.method == 'GET':
          return render_template('index.html')
     else:
          if "url" in request.form and "name" in request.form:
               URL = request.form.get("url")
               if validators.url(URL):
                    org_name = request.form.get("name")
                    trans = {}
                    trans['url'] = URL
                    trans['organization_name'] = org_name
                    try:
                         table.put_item(Item=trans)
                    except:
                         mesg = 'Data Insertion Not Successfull'
                    mesg = 'Data Saved Successfully'
               else:
                    mesg = 'Not a valid URL' 
          else:
               mesg = 'Incomplete Form Data Submitted'


          return render_template('index.html',result = mesg)

HTML CODE

<body>
                    <!-- Navbar-->
        <nav class="navbar navbar-dark bg-dark">
            <span class="navbar-brand mb-0 h1">SCRAPE DATA</span>
        </nav>
        <br>
        <h3 style="margin: auto;width: 80%; text-align: center; margin-top: 20px;  text-transform: uppercase">SCRAPE DATA</h3>

        <div class="container">
            <form action = "{{url_for('parseurl')}}" method ="POST" >
                <div class="form-group">
                    <label for="name">Enter Organization Name</label>
                    <input type="text" class="form-control" id="name" name="name" required>
                    <label for="url">Enter URL to scrape</label>
                    <input type="text" class="form-control" id="url" name="url" required>
                </div>
            
                <br>
                <div class="form-group">
                    <input class="btn btn-primary" type="submit" value="Scrape">
                </div>
                <div id ="result">
                    <strong style="color:red">{{result}}</strong>
             </div>
            </form>
        </div>
</body>
Aniiya0978
  • 274
  • 1
  • 9
  • 1
    Redirecting after saving the data would help, or check some of the ideas mentioned here - https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr – Peter Sep 29 '21 at 12:13

1 Answers1

0

When you clicked submit button, your code parseurl() function is called with the post method and else block executing. In final of else block, return render_template so page load with POST.

Page refresh sends the last request again. Your page load with POST request, refresh process sends the same request again and the same data is resubmitted.

You should replace the else block's return with return redirect(url_for('parseurl')).

But in this case, the form content will be reset when the page is reloaded. If the operation is not successful (in cases where you return an error message), you do not want the form to be reset. For this, you should use redirect only if the operation is successful.

As a result, your code should look something like this:

@app.route('/',methods = ['GET','POST'])
def parseurl():
    if request.method == 'GET':
        return render_template('index.html')
    else:
        if "url" in request.form and "name" in request.form:
            URL = request.form.get("url")
            if validators.url(URL):
                org_name = request.form.get("name")
                trans = {}
                trans['url'] = URL
                trans['organization_name'] = org_name
                try:
                    table.put_item(Item=trans)
                    mesg = 'Data Saved Successfully'
                    return redirect(url_for('parseurl'))
                except:
                    mesg = 'Data Insertion Not Successfull'
            else:
                mesg = 'Not a valid URL' 
        else:
            mesg = 'Incomplete Form Data Submitted'


        return render_template('index.html',result = mesg)