I am new in build python api. I want to get the data depend on date.
I am following this link to code my api but I don’t know what can I do in execute.
This is my python code:
# -*- coding: utf-8 -*- from flask import Flask, render_template, request from flask_mysqldb import MySQL app = Flask(__name__) app.config["DEBUG"] = True app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = 'root' app.config['MYSQL_DB'] = 'MyDB' app.config['MYSQL_charset'] ='utf8' mysql = MySQL(app) @app.route('/',methods = ['GET','POST']) def index(): if request.method == "POST": details = request.form firstday = details['firstday'] lastday = details['lastday'] cur = mysql.connection.cursor() cur.execute("select * from transaction join transaction_item on transaction.id = transaction_item.transaction_id join customer on customer.id = transaction.customer_id where transaction_datetime <= lastday and transaction_datetime >= firstday VALUES (%s, %s)", (firstday, lastday)) mysql.connection.commit() cur.close() return 'success' return render_template('index.html') if __name__ == '__main__': app.run()
This is my HTML code:
<HTML> <BODY bgcolor="cyan"> <form method="POST" action=""> <center> <H1>Enter your details </H1> <br> first_transaction_datetime <input type = "text" name= "firstday" /> <br> last_transaction_datetime <input type = "text" name= "lastday" /> <br> <input type = "submit"> </center> </form> </BODY> </HTML>
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
When you are sending requests to a back-end it does not fill out the form an easy way to do it would be to add the query values in the URL
then your python code would look something like this.
@app.route('/',methods = ['GET','POST']) def index(): firstName = request.args.get('firstName') lastName = request.args.get('lastName') if request.method == "POST": # Do stuff here
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂