I am currently creating a web page with Jquery and Flask. I am sending inputs from ajax and get input values in Flask. When Flask receives the values, a module, Instabot, sends a message. I would like to automatically reload the page after Instabot sends a message.
I tried to reload the page in form.js but then the input values are not sent to a DM. I also tried to redirect the page 'index.html' by using redirect('index.html'), render_template('index.html'), redirect(url_for('index.html')). Yet, those are not working.
This is index.html
<form class="form-inline">
<div class="row">
<div class="form-group col-md-6">
<input style="text-align-last: center;" type="text" name="name" class="form-control" id="name" placeholder="Your Name">
</div>
<div class="form-group col-md-6 mt-3 mt-md-0">
<div style="position: relative;"><input style="background: white; text-align-last: center;" type="text" class="form-control" id="datetimepicker" placeholder="Pick Date and Time"></div>
</div>
</div>
<br>
<div class="text-center"><button id="submit_button" type="submit" class="btn btn-success">Send Message</button></div>
</form>
Here is form.js for ajax.
$(document).ready(function() {
flatpickr("#datetimepicker",
{
enableTime: true,
dateFormat: "Y-m-d H:i K",
minDate: "today",
disableMobile: "true"
});
$('form').on('submit',function(event) {
var name = $('#name').val();
var datetime = $("#datetimepicker").val();
$.ajax({
data: {
name: name,
datetime: datetime
},
url: '/contact',
type: 'POST',
dataType: 'text',
success: function(data) {
if(data.error) {
alert('Error!');
} else {
location.reload(); // this is what I tried
}
}
});
event.preventDefault();});});
This is contact.py:
from flask import Flask, render_template, request, jsonify, send_from_directory,
redirect, url_for
from instabot import Bot import os
import glob
import time
bot = Bot()
app = Flask(__name__, template_folder='templates')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/contact', methods=['POST', 'GET'])
def contact():
name = request.form['name']
datetime = request.form['datetime']
# Login using bot.
bot.login(username="myInstaID",
password="myInstaPW", is_threaded=True)
urer_ids = "Friend's InstaID"
text = "New Appointment: \n Name: {} \n Date: {}".format(name, datetime)
# Sending messages
bot.send_messages(text, urer_ids)
return redirect('/') // this is I tried to solve my issue
if __name__ == '__main__':
app.run(debug=True)
I am stuck here for a month. I have no idea. Please help me. Thank you in advance.