如何使用两种不同形式的同一视图,并且不丢失用户提供的数据?

2024-04-24 21:23:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表格,上面有几个关于会议的输入字段。你知道吗

@app.route('/Alt_Reuniao/<WCodigoChaveP>', methods=['GET', 'POST'])

def Alt_Reuniao(WCodigoChaveP):

    # This function returns a list with de data from the Database about a specific meeting

    WReuniao = Executa_Query_Leitura("0005", WCodigoChaveP, "O")

    if not WReuniao:

        flash('Error.......')

        return redirect(url_for('Cad_Reunioes'))

    else:

        if WReuniao[8]:

            # this function returns a list with de data from the Database about the topic of the meeting 

            WAssunto = Executa_Query_Leitura("0002", WReuniao[8], "O")


        Wform = Cad_Reunioes_Form()

        if request.method == "POST":

            if Wform.validate():

                # save the data ......

                return redirect(url_for('Cad_Reunioes'))

            else:

                for Werro in Wform.errors.values():

                    flash(Werro[0])

                return render_template('Reuniao.html', Wformulario=Wform)

        else:

            Wform.WCPO_Reuniao.data = WReuniao[1]

            Wform.WCPO_Desc_Reuniao.data = WReuniao[2]

            Wform.WCPO_Nro_Part.data = WReuniao[3]

            Wform.WCPO_Cod_Assunto.data = WReuniao[8]

            if WReuniao[8]:

                if WAssunto:

                    Wform.WCPO_Assunto.data = WAssunto[1]


            return render_template('Reuniao.html', Wformulario=Wform)

这是我的团购.html模板:

{% extends "Base_Cad_2.html" %}

{% block dados %}

    {{ Wformulario.WCPO_Reuniao.label(id="WCPO_Reuniao", class="lab1") }} {{ Wformulario.WCPO_Reuniao(size = 100, maxlength=30, id="WCPO_Reuniao") }}

    <p id="PL"> {{ Wformulario.WCPO_L_Desc_Reuniao(id="WCPO_L_Desc_Reuniao", class="lab1") }} </p>

    {{ Wformulario.WCPO_Desc_Reuniao(rows=5, cols=100, id="WCPO_Desc_Reuniao") }}

    {{ Wformulario.WCPO_Nro_Part.label(id="WCPO_Nro_Part", class="lab1") }} {{ Wformulario.WCPO_Nro_Part(size = 5, id="WCPO_Nro_Part") }}

    {{ Wformulario.WCPO_Cod_Assunto.label(id="WCPO_Cod_Assunto", class="lab1") }} {{ Wformulario.WCPO_Cod_Assunto(size=10, readonly='readonly', id="WCPO_Cod_Assunto") }}

    {{ Wformulario.WCPO_Assunto.label(id="WCPO_Assunto", class="lab1") }} {{ Wformulario.WCPO_Assunto(size=95, readonly='readonly', id="WCPO_Assunto") }}

    <button id="Selec_Assunto" name="Selec_Assunto" value="Selec_Assunto" type="button"> <a class="botoes" href="{{ url_for('Selec_Assuntos_Inicio', WRotChama = "001", WCodorig = Wformulario.WCPO_Cod_Reuniao ) }}" hreflang="pt-br"> Seleciona </a> </button>

{% endblock %}

{% block botoes %}

    <button id="gravar" name="gravar" value="Gravar" type="submit" class="botoes" > Gravar </button>

{% endblock %}

基本上,这个观点很好。你知道吗

当我从上一个模板中的列表中选择一个会议时,view方法是GET,数据库中的数据被传递到表单,模板正确呈现。你知道吗

当方法是POST时,表单中的数据将正确地保存在数据库中

表单上有一个选择按钮。当用户单击该按钮时,我指向一个视图,该视图呈现一个模板,其中包含会议所有可能主题的列表。这些主题来自数据库。可能有很多,所以我不能只使用一个组合。这就是为什么我使用模板。你知道吗

当用户从列表中选择一个主题时,我必须再次呈现Alt\u Reuniao视图,并且必须将所选主题传递给视图。你知道吗

这很有效。你知道吗

我的问题是:这个方法又是一个GET。如果在点击SelecèAssunto按钮之前,用户更改了表单中其他字段的数据或在表单中输入了数据,那么当选择了主题时,用户将丢失这些数据。视图再次使用数据库中的数据呈现模板。你知道吗

一切似乎都很顺利。我只想在点击Selec\u Assunto按钮之前维护用户在表单中已经更改的数据。你知道吗

正如你所看到的,我是网络开发的新手,Python,Flask

非常感谢你的帮助。你知道吗


Tags: 数据模板id表单dataifclasscod
1条回答
网友
1楼 · 发布于 2024-04-24 21:23:03

在这种情况下,您可以更新“Selec\u Assunto”按钮行为以将表单数据发布回服务器,但也可以包含重定向变量。当包含重定向变量时,服务器将保存表单更改,然后重定向到“Selec\u Assuntos\u Inicio”视图,而如果不存在重定向变量,它将遵循上一个/正常的表单发布行为。例如:

    if request.method == "POST":
        if Wform.validate():

            # save the data ……

            if Wform.redirect.data:
                return redirect(Wform.redirect.data)
            else:
                return redirect(url_for('Cad_Reunioes'))

        else:

            for Werro in Wform.errors.values():
                flash(Werro[0])

            return render_template('Reuniao.html', Wformulario=Wform)

值得注意的是,这种方法要求您使用JavaScript来重写“SelecèAssunto”按钮行为(因为您实际上是在强制它执行表单提交)。使用jQuery有一种方法:

$('button#Selec_Assunto').on('click', function() {
    $('form#formId').append('<input type="hidden" name="redirect" value="{{ url_for('Selec_Assuntos_Inicio', WRotChama = "001", WCodorig = Wformulario.WCPO_Cod_Reuniao ) }}">');
    $('form#formId').submit();
});

也就是说,从编码和可用性的角度来看,一个可能更好的选择是将主题数据异步加载到现有页面中,这样就不必重新加载视图。这样可以避免临时保存表单数据。你知道吗

相关问题 更多 >