datetime-local returns NoneType when it is post to flask

Issue

I would like the user to input the DateTime and Flask app to get this info, but "DateTime-local" returns None when it is posted to Flask

HTML:

    <form action="/" method="POST">
        <div style=font-size:15px; font-weight:bold; mergin-left:150px;>
            <p>start<br><input type="datetime-local",name="time_start"></p>
            <p>end<br><input type="datetime-local",name="time_end"></p>
            <p><input type="submit" value="submit"></p>
        </div>
    </form>

Flask:

@app.route('/',methods=["GET","POST"])
def hello_world():
    if request.method == "POST":
        st=request.form.get("time_start")
        ed=request.form.get("time_end")
        print(st,type(st))
        print(ed,type(ed))

that returns:

None <class 'NoneType'>
None <class 'NoneType'>

I don’t understand why… Could anyone help me get the DateTime value from HTML?
I tried Chrome and Edge browser, neither worked.

Solution

Now I found a solution. Setting ‘value’ attribute (as the default time) returns the expected value.

<input type="datetime-local" value="2021-10-19T10:00">            

Answered By – Ted

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published