I want to pass through a button a parameter which is its input value and save it in the data base. More specifically I want to add a message into a conversation.
My html template looks like this: (Index.html)
<form action="/addMessage/{{id_conver}}" class="bg-light" method="POST">
<div class="input-group">
<input type="text" placeholder="Type a message" name="message" aria-describedby="button-addon2"
class="form-control rounded-0 border-0 py-4 bg-light">
<div class="input-group-append">
<input type="submit" class="btn btn-info">
</div>
</div>
</form>
where id_conver is a id of a conversation.
In my programm, web.py I call the API to insert the value given in the input with name=message:
def add_message(id):
message = request.form['message']
response= requests.post("http://127.0.0.1:3000/addMessage/%s" %(id), params={'message': message})
return render_template('index.html')
And this is the API code: Api.py
@app.route('/addMessage/<id>', methods=['POST'])
def addMessage(id):
message = request.get_json('message')
if(message is None):
flash("Message NOT inserted")
return 'Fail'
else:
cur = mysql.connection.cursor()
cur.execute('INSERT INTO message (time_stamp, message, id_conver, id_agent) VALUES (%s, %s, %s, %s)',
(datetime.now(), message, id, 1))
mysql.connection.commit()
flash('Message Added successfully')
return redirect(url_for('Index'))
I have tried different approaches to get the parameter right in the API but failed. I am new to python. Any help would be appreciated.
Thank you in advance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…