Python确认弹出框,保留上一页中的变量

2024-04-25 23:56:20 发布

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

编辑:

为了简化这个问题,我有以下代码:

@app.route('/modify', methods=['GET', 'POST'])
def modify():
    if request.method == 'GET':
        print('get')
        print(request.args.get("port_selection"))


    elif request.method =='POST':
        print('post')
        print(request.form['port_input'])

    return render_template('modify.html', title='Modify Interface')

对于get请求,尝试打印端口\选择只会显示为“无”。这是我输入值的文本框,然后单击提交按钮,它将弹出一个模式


我有一个烧瓶应用程序,我希望发生以下情况:

在某些文本框中输入文本,然后单击提交。 然后会出现一个弹出框,您可以在其中取消或确认提交

我的问题是,当弹出框出现时,它会删除文本框中设置的变量,因此值被设置为“无”。 我不确定该将按钮方法设置为、POST还是GET。 如果我使用POST,那么变量会被保存,但是页面只会加载下一个render模板。我不知道如何让它等待确认

我的html如下:

<div class="content-section">
<div class="form-row">
  <div class="col">
    <div class="input-group-prepend">
      <span class="input-group-text">Gi1/0/</span>
    <input type="text" class="form-control" name="port_selection" placeholder="Port Number">
    </div>
  </div>
  <div class="col">
    <input type="text" class="form-control" name ="vlan_text" placeholder="VLAN">
<!-- # Button trigger modal -->

    <input class="btn btn-info pull-right" method="GET" type="submit" data-toggle="modal" data-target="#vlan_confirm" name="submit_vlan_changes" value="Change VLAN">


    <!-- Modal -->

    <div class="modal fade" id="vlan_confirm" tabindex="-1" role="dialog" aria-labelledby="vlan_confirm" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">

            <h5 class="modal-title" id="vlan_confirm">Please Confirm Changes</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body" >
            ...
          </div>
          <form method="POST" action="">

          <div class="modal-footer">
            <button type="submit" name="cancel_vlan" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
            <button type="submit" name="modify_vlan" class="btn btn-primary">Commit changes</button>
          </div>
        </div>
      </div>
    </div>
      </div>
    </div>
      <div class="well">
  <!-- # the <pre> tags specify preformatted text-->
<pre> {{ output }} </pre>

页面的应用程序路径如下:

@app.route('/modify', methods=['GET', 'POST'])
def modify():
    if request.method == 'POST':
        #todo add in redirect to login page for when netmiko times out 
        if "sh_run_int" in request.form:
            port_vlan['port'] = request.form['port_input']
            net_connect = Netmiko(**myDevice)
            net_connect.enable()
            show_run_int = net_connect.send_command('show run int fa1/'+ port_vlan['port'])
            if 'Invalid input detected' in show_run_int:  # Checks for valid port selection
                flash(f'Invalid input, check port number is valid', 'warning')
                return render_template('modify.html', title='Modify Interface')
            elif 'switchport mode trunk' in show_run_int:
                flash(f'You cannot modify trunk ports', 'warning')
                return render_template('modify.html', title='Modify Interface', show_run_int = show_run_int)


        #todo enter a confirm selection popup with the ability to back out of change
        elif "modify_vlan" in request.form:
            net_connect = Netmiko(**myDevice)
            net_connect.enable()
            show_run_int2 = net_connect.send_command('show run int fa1/'+ port_vlan['port'])
            showVlan = net_connect.send_command('show vlan-switch brief')
            print(port_vlan['vlan'])


            if 'Invalid input detected' in show_run_int2:  # Checks for valid port selection
                flash(f'Invalid input, check port number is valid', 'danger')
                return render_template('modify.html', title='Modify Interface')
            elif 'switchport mode trunk' in show_run_int2:
                flash(f'Error - You cannot modify trunk ports!', 'danger')
                return render_template('modify.html', title='Modify Interface')
            elif port_vlan['vlan'] not in showVlan:
                flash(f'VLAN does not exist!', 'danger')
                return render_template('modify.html', title='Modify Interface')                
            else:
                config_commands = [  # config_commands list array.
                    'Interface fa1/' + port_vlan['port_selection'],
                    'switchport access vlan ' + port_vlan['vlan']
                ]
                net_connect.send_config_set(config_commands)
                output = net_connect.send_command('show run int fa1/'+ port_vlan['port_selection'])
                print(output)
                flash(f'Port updated successfully!', 'success')  # Show Updated Config after changes
                return render_template('modify.html', title='Modify Interface', output=output)

    return render_template('modify.html', title='Modify Interface')


@app.route('/usage')
def usage():
    return render_template('usage.html', title='Usage')

任何帮助都将不胜感激,因为我目前还不能弄清楚这一点


Tags: rundivinputreturntitleporthtmlshow

热门问题