0

I wrote a simple form. For some reason, the form sends always GET requests instead of POST.

CODE EXAMPLE

  <form action="{{ url_for('home') }}" method="post">
                    <div class="row">
                        <div class="col-sm-12">
                            <input type="text" required dir="rtl" name="name" class="form-control" id="Name" placeholder=" שם ">
                        </div>
                        <div class="col-sm-12">
                            <input type="tel" required dir="rtl" name="phone" class="form-control" id="Phone" placeholder=" פלאפון ">
                        </div>
                        <div class="col-sm-12">
                            <input type="email" dir="rtl" name="email" class="form-control" id="Email" placeholder=" דוא'ל ">
                        </div>
                        <div class="col-sm-12">
                            <textarea class="form-control" dir="rtl" name="text" rows="5" id="Message" placeholder=" הודעה "></textarea>
                        </div>
                        <div class="col-sm-12 col-md-12 c-c-padding">
                            <button type="submit" name="send" id="contact_submit" class="btn btn-dm"> שלח </button>
                        </div>
                    </div>
   </form>                

SERVER SIDE

@app.route('/', methods=["GET", "POST"])
def home():
    print(request.method)
    if request.method == 'POST':
        name = request.form['name']
        phone = request.form['phone']
        email = request.form['email']
        print(name + phone + email)
        if phone:
            requests.post('DOMAIN',
                auth=("api", 'API'),
                data={"from": "Email",
                      "to": ["email"],
                      "bcc": "",
                      "subject": " ",
                      "text": """  Name: {} 
                                   Phone: {}
                                   Email: {} """.format(name, phone, email)})
            flash(' welldone ', 'success')
            return redirect(url_for('home'))
        else:
            flash(' phone ', 'warning')
            return redirect(url_for('home'))
    return render_template('index.html')

After I press the 'submit' button. As I said, it sends GET request instead of a POST. Then something weird happens. It injects 'index.html' into the form - and what happens, is that I have a website inside the website.

First, what should I do to fix the problem of sending POST request instead of GET request. Second, why 'render_template()' injects the HTML into the form? Instead render the whole page?

E.Bolandian
  • 553
  • 10
  • 35

1 Answers1

0

Replace

return redirect(url_for('home'))

with

return redirect(request.url)

or

return redirect(request.referrer)

Why? If we read the docstring for url_for():

Generates a URL to the given endpoint with the method provided

We see that redirect(url_for('home')) tells Flask to redirect the client to the home() endpoint, which then calls redirect(url_for('home')) and so on in a looping cycle. When needed, call redirect(url_for('home')) from endpoints different than home().

From the docstrings for request.url and request.referrer:

  • request.url :

The full request URL with the scheme, host, root path, path, and query string.

  • request.referrer :

The Referer[sic] request-header field allows the client to specify, for the server's benefit, the address (URI) of the resource from which the Request-URI was obtained (the "referrer", although the header field is misspelled).

You can read more about them from these SO answers.

Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16