[See 2nd edit at bottom of question]
[EDIT: This question was marked as an "exact" duplicate of two previous questions/answer to which links were provided. I've read both now carefully a few times, and I can't see where they address my issue at all. The methods they do address--how to send form data to the server using JS/AJAX/jquery may be useful if it turns out there's no way to get Flask to process the data from a post request without returning a new page to the client. To clarify: I'm *not* trying to get the form data to change something on its own page. I can see that I could leave Flask out of that and just use JS, but that's not what I want to happen. The fact is, I don't want any changes made to the page itself (except launch a modal using Bootstrap/jquery), I just want to send the form data to Flask and have it do something with it. So, here's my original question again ]:
I am using Python (3.6.4) Flask and Flask-Mail on a web page that uses Bootstrap for the frontend. After Flask sends an email from a contact form on the page, I want Bootstrap to launch a "modal" popup over the existing page to acknowledge the submission. Once the user clicks on the "OK" button on the modal, the popup should disappear with nothing else changed on the page, including its scrolled position (the form is at the bottom of the page). BTW, the same button that submits the contact form should also launch the modal--I've tested that separately. I've also thoroughly tested the form and it's sending emails as programmed (and with all the usual settings for mail in my config.py module). Here's the code in my routes.py module:
@app.route('/process_contact', methods=['POST'])
def process_contact():
name = request.form['name']
email = request.form['email']
message = request.form['message']
text = 'From: ' + name +'\n\nEmail: ' + email + '\n\nMessage: ' + message
html = 'From: ' + name +'<p>Email: ' + email + '<p>Message: ' + message
send_email('New contact form message', 'contactform@pdxspot.com',
['contact@pdxspot.com'], text, html,)
Now, in order to test it without werkzeug errors, I had to "return" something, so I tacked on:
return 'Name: ' + name + '/ Email: ' + email + '<hr>Message: ' + message
That of course took me to a new page containing only the concatenations. With this work-around, I was able to confirm the emails were being sent to an external server just as they should.
The thing is, I don't want to "return" a different page. I want the existing page to stay in place, and the Bootstrap modal to do its thing. In other words, all I want my decorated process_contact() function to do is to process and send the email and--for the time being--leave the "view" to be managed by Bootstrap. Similar questions and answers here on SO seem to orbit around my issue, but none centered on it. Is there a way I can do this? Thanks.
[2nd EDIT]: I found an answer to my question and a solution to my problem here: Creating a view function without returning a response in Flask. It's simple. Just "return" an empty string and status-code 204:
return '', 204
Nothing is visibly changed on client side, and now my Bootstrap modal is triggered by the submit button exactly the way I wanted. Thanks for the replies and to Grey Li for his answer.