简体   繁体   中英

Prevent Flask + jinja2 from converting a long integer into a decimal

[a total python noob]
I'm rendering a template and passing in a dictionary:

d = {'id':3423444989845}

but when I'm doing like so <div id="{{d['id']}}"> instead of getting <div id="3423444989845"> the HTML renders like this: <div id="3.423445e+12"> .

How can I prevent this? (my temporary solution was to pass the id as a string, but I was hoping to keep it an integer.

Thanks

The issue is that your ID is a float, not an integer (JavaScript's Date#getTime returns a float and I'm willing to wager that MongoDB is also storing the value as a float.) If you want it to be treated as an integer you should convert the value you are given by JavaScript to an integer in your Python code:

if request.method == "POST":
    try:
        # Remember, *never* trust user input
        # This is most likely a number but you are not guaranteed that
        data_id = int(request["id"])
    except ValueError:
        abort(400)
    # If we got here, we have a valid int
    # Insert data into MongoDB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM