[Python] Switching from Flask to Gunicorn introduces odd DB behavior

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm attempted to describe the problem without barfing out massive amounts of code, but...

    I have an app that works correctly under Flask. Part of the app is a series of DB queries to return a list of objects. The example I'm going to show is from a page that displays a list of all the objects in the DB along with a photo for each object. The photo is stored on the server as a file with a DB entry that points to the file location.

    The relevant Python code for the query and return:

    @app.route("/inventory")


    def inventory():

    global ITEM_IMAGE_DIR
    ### SET UP QUERY
    offset = 0
    limit = 30
    inv_query = """ SELECT * FROM items ORDER BY item_num LIMIT ? OFFSET ? """

    ### QUERY DB
    cursor = mydb.cursor(prepared=True)
    cursor.execute(inv_query,(limit,offset))
    result = cursor.fetchall()
    cursor.close()
    item_list = result

    ### GET COOKIE AND SHOW THE APPROPRAITE VIEW
    current_view = request.cookies.get("view")
    if current_view == "mobile":
    return render_template(
    "inventory_mobile.html",
    item_list=item_list,
    offset = offset,
    ITEM_IMAGE_DIR=ITEM_IMAGE_DIR
    )

    else:
    return render_template(
    "inventory.html",
    item_list=item_list,
    offset = offset,
    ITEM_IMAGE_DIR=ITEM_IMAGE_DIR
    )


    And in the template:

    <span class="table_body">
    {%- for item_num, item_name, box_num, item_pic, item_date, item_cat, item_desc in item_list %}
    <div class= "table_row">
    <div class= "table_cell" style="border-bottom: 1px solid black;">
    {{ item_name }}

    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">&nbsp;
    <img src="{{ITEM_IMAGE_DIR}}{{item_pic}}" class="thumbnail" alt="item image"
    style="display:inline-block; max-width:230px; max-height:50px; width: auto; height: auto;">
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    {{ item_date|string }}
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    {{ item_cat }}
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    {{ box_num }}
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;" >
    <div>
    <form action="/itemedit/{{item_num}}">
    <button type="submit" id="edit_submit_button_{{loop.index}}"
    class="ico_imps ico_edit" >
    </button>
    </form>
    </div>
    </div>
    </div>
    {%- endfor %}
    </span>


    The problem:

    When this runs under Flask, it works correctly. The render of the HTML page contains the correct {{item_pic}} entry as: /static/images/items/curtain1720642687.561024.jpg The image displays correctly because the returned value is the correct file location for the curtain image.

    <div class= "table_row">
    <div class= "table_cell" style="border-bottom: 1px solid black;">
    Curtain

    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">&nbsp;
    <img src="/static/images/items/curtain1720642687.561024.jpg" class="thumbnail" alt="item image"
    style="display:inline-block; max-width:230px; max-height:50px; width: auto; height: auto;">
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    2024-05-12
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    Decorating
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    2
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;" >
    <div>
    <form action="/itemedit/3">
    <button type="submit" id="edit_submit_button_2"
    class="ico_imps ico_edit" >
    </button>
    </form>
    </div>
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    <div>
    <form action="/itemdel/3">
    <button type="submit" id="del_submit_button_2"
    class="ico_imps ico_trash">
    </button>
    </form>
    </div>
    </div>
    </div>


    However, when I'm using Gunicorn the DB entry for the file location used for {{item_pic}} is returned as /static/images/items/bytearray(b'curtain1720642687.561024.jpg Which obviously is a broken link.

    <div class= "table_row">
    <div class= "table_cell" style="border-bottom: 1px solid black;">
    Curtain

    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">&nbsp;
    <img src="images/items/bytearray(b'curtain1720642687.561024.jpg" class="thumbnail" alt="item image"
    style="display:inline-block; max-width:230px; max-height:50px; width: auto; height: auto;">
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    2024-05-12
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    Decorating
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    2
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;" >
    <div>
    <form action="/itemedit/3">
    <button type="submit" id="edit_submit_button_2"
    class="ico_imps ico_edit" >
    </button>
    </form>
    </div>
    </div>
    <div class="table_cell" style="border-bottom: 1px solid black;">
    <div>
    <form action="/itemdel/3">
    <button type="submit" id="del_submit_button_2"
    class="ico_imps ico_trash">
    </button>
    </form>
    </div>
    </div>
    </div>


    I've been around the Google search block several times and can't find any relevant clues as to why this difference is occurring. If someone here could help point me in the correct direction it would be much appreciated.

    I tried running the code under gunicorn but a byte array was returned

    Continue reading...

Compartilhe esta Página