SimpleTemplate shows only part of format_now even if value={{format_now}}

Asked 2 years ago, Updated 2 years ago, 58 views

I would like to use datetime.strftime in the SimpleTemplate of Bottle to match the value in the form to the pattern specified in the pattern attribute

If strftime is executed, the value of format_now should match the pattern of the form.
However, if you actually access it with a browser, you will only see the Western calendar and date as shown in Figure 1
Why is that?

import datetime
import bottle

jst=datetime.timezone(datetime.timedelta(hours=9), "JST")

@bottle.route("/")
def index():
    now=datetime.datetime.now(jst)
    return bottle.template("template.tpl", {"now":now})

template.tpl:

%format_now=now.strftime("%Y-%m-%d%I:%M:%S%p")

<input type="text" pattern="\d{4}-\d{1,2}-\d{1,2}-\d{1,2}(1[0-2]|0[0-9]:[0-5][0-9]:[0-5][0-9](PM|AM)?" name="now"value={{format_now}}}}>

Figure 1
Actual Results

python bottle

2022-09-30 21:12

1 Answers

value attribute value is not quoted.
As you can see by displaying the HTML source after generation,

<input type="text"...value=2016-08-08 11:14:20 PM>

Because of the , only the first space is considered a value of value and only the date and time are displayed.
value="{{format_now}}" (enclosed in quotation).


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.