python flask input URL (date,diff, operation-add,del) how can I correct

Issue

This Content is from Stack Overflow. Question asked by 俠客行

I want to input the date in flask and can calculate the date, I searched that we can use the function below but what do I missed to correct.

from datetime import date

date = datetime.datetime.strptime(date, "%m/%d/%y")

new_date = date + datetime.timedelta(days= diff)

python flask input URL (date,diff, operation-add,del) how can I correct

example URL input:
http://127.0.0.1:5000/data?dtae=2022-01-01&diff=5&op=a{add,del}

http://127.0.0.1:5000/data?dtae=2022-01-01&diff=13&op=add

http://127.0.0.1:5000/data?dtae=2022-01-01&diff=5&op=del

optput:
the 13 days later is 2022-01-14
the 5 days back is 2021-12-27

# import main Flask class and request object
from flask import Flask, request
from datetime import date,  datetime, time ,timedelta

# create the Flask app
app = Flask(__name__)
 
 
@app.route('/data')
def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    # if key doesn't exist, returns a 400, bad request error
    CustomerID = request.args['CustomerID']
 
    # if key doesn't exist, returns None
    date = request.args.get('date')
    
    diff = request.args.get('diff')
    # take input from string to other variable變數
 
    CustomerID_float = float(CustomerID)
   
    CustomerID_type = type(CustomerID)
    name_type = type(name)
 
    print(type(name))

    
    date = datetime.datetime.strptime(date, "%m/%d/%y")

    new_date = date + datetime.timedelta(days= diff)
 
    new_date = date
 
    return '''
              <h1>The name is: {}</h1>
              <h1>The CustomerID is: {}</h1>
              <h1>The CustomerID float is: {}</h1>
 
 
              <h1>The date is: {}</h1>
 
              <h1>The new_date is: {}'''.format(name, CustomerID, CustomerID_float, date , new_date)
             
 
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?