为什么 render_template 继续显示烧瓶形式的旧值?

Why does the render_template keep on showing the old value of the flask form?

几个小时以来我一直在寻找答案。如果我遗漏了什么,我深表歉意。

我多次使用同一个表单来向我的数据库中添加行。 每次我检查 excel 文件以使用用户可能想要更改的已知信息预填充一些 wtforms StringFields 时。

问题是:我更改了 form.whatever.data 并在打印时显示了新值。但是当我渲染模板时,它一直显示旧值。 为了以防万一,我尝试在为它分配新值之前执行 form.hours_estimate.data = "" 但它没有用。

我会在这里附上我正在谈论的路线。重要的一点是在 # Get form ready for next service 之后。如果需要更多信息,请告诉我。

非常感谢。

@coordinator_bp.route("/coordinator/generate-order/<string:pev>", methods=['GET', 'POST'])
@login_required
def generate_order_services(pev):
    if not (current_user.is_coordinator or current_user.is_manager):
        return redirect(url_for('public.home'))

    # Get the excel URL
    f = open("./app/database/datafile", 'r')
    filepath = f.read()
    f.close()

    error = None

    if GenerateServicesForm().submit1.data and GenerateServicesForm().validate():
        # First screen submit (validate the data -> first Service introduction)
        form = FillServiceForm()

        next_service_row = get_next_service_row(filepath)
        if next_service_row is None:
            excel_info = excel_get_pev(filepath)
            error = "Excel error. Service code not found. If you get this error please report the exact way you did it."
            return render_template('coordinator/get_pev_form.html', form=GetPevForm(), error=error, info=excel_info)

        service_info = get_service_info(filepath, next_service_row)
        service_code = service_info[0]
        start_date = service_info[1]
        time_estimate = service_info[2]

        objects = AssemblyType.get_all()
        assembly_types = []
        for assembly_type in objects:
            assembly_types.append(assembly_type.type)

        form.service_code.data = service_code
        form.start_date.data = start_date
        form.hours_estimate.data = time_estimate

        return render_template('coordinator/fill_service_form.html', form=form, error=error, assembly_types=assembly_types)

    if FillServiceForm().submit2.data:
        if not FillServiceForm().validate():
            objects = AssemblyType.get_all()
            assembly_types = []
            for assembly_type in objects:
                assembly_types.append(assembly_type.type)
            return render_template('coordinator/fill_service_form.html', form=FillServiceForm(), error=error,
                                   assembly_types=assembly_types)

        # Service screen submits
        # Here we save the data of the last submit and ready the next one or end the generation process

        # Ready the form
        form = FillServiceForm()
        next_service_row = get_next_service_row(filepath)
        if next_service_row is None:
            excel_info = excel_get_pev(filepath)
            error = "Excel error. Service code not found. If you get this error please report the exact way you did it."
            return render_template('coordinator/get_pev_form.html', form=GetPevForm(), error=error, info=excel_info)

        service_info = get_service_info(filepath, next_service_row)
        service_code = service_info[0]
        form.service_code.data = service_code

        # create the service (this deletes the service code from the excel)
        service = create_service(form, filepath)
        if isinstance(service,str):
            return render_template('coordinator/fill_service_form.html', form=form, error=service)

        # Get next service
        next_service_row = get_next_service_row(filepath)
        if next_service_row is None:
            # This means there is no more services pending
            return "ALL DONE"
        else:
            # Get form ready for next service
            service_info = get_service_info(filepath, next_service_row)
            service_code = service_info[0]
            start_date = service_info[1]
            time_estimate = service_info[2]
            print("time_estimate")
            print(time_estimate) # I get the new value.
            objects = AssemblyType.get_all()
            assembly_types = []
            for assembly_type in objects:
                assembly_types.append(assembly_type.type)

            form.service_code.data = service_code
            form.start_date.data = start_date
            form.hours_estimate.data = time_estimate
            print(form.hours_estimate.data) # Here I get the new value. Everything should be fine.

            # In the html, the old value keeps on popping.
            return render_template('coordinator/fill_service_form.html', form=form, error=error,
                                   assembly_types=assembly_types)


    number_of_services = excel_get_services(filepath=filepath, selected_pev=pev)
    # Get the number of the first excel row of the selected pev
    first_row = excel_get_row(filepath, pev)
    if first_row is None:
        excel_info = excel_get_pev(filepath)
        error = "Excel error. PEV not found. If you get this error please report the exact way you did it."
        return render_template('coordinator/get_pev_form.html', form=GetPevForm(), error=error, info=excel_info)

    service_code = []
    start_date = []
    time_estimate_code = []
    quantity = []

    # Open the excel
    wb = load_workbook(filepath)
    # grab the active worksheet
    ws = wb.active

    for idx in range(number_of_services):
        # Append the data to the lists
        service_code.append(ws.cell(row=first_row+idx, column=12).value)
        start_date.append(str(ws.cell(row=first_row + idx, column=5).value)[:10])
        time_estimate_code.append(ws.cell(row=first_row+idx, column=7).value)
        quantity.append(ws.cell(row=first_row + idx, column=9).value)

    wb.close()

    return render_template('coordinator/generate_services_form.html',
                           form=GenerateServicesForm(),
                           pev=pev,
                           service_code=service_code,
                           start_date=start_date,
                           time_estimate_code=time_estimate_code,
                           quantity=quantity)

好吧,我找到了一个解决方法:我像这样在表单外发送数据:

            return render_template('coordinator/fill_service_form.html', form=form, error=error,
                                   assembly_types=assembly_types,
                                   service_code=service_code,
                                   start_date=start_date,
                                   time_estimate=time_estimate)

并为此替换 jinja 形式:

<input class="form-control" placeholder="2021-04-23" name="start_date" type="text" value="{{start_date}}">

我还在使用表单(name=表单域名称),同时在外部输入值

我希望这对某人有所帮助。