Flask WTF - 一页上有多个表格

时间:2015-05-24 02:45:25

标签: python flask wtforms

在我的页面上,我有5个不同的文本框,我希望当用户提交按钮时,包含其中数据的文本框会被发布。

PYTHON:

from flask import Flask, request, redirect, url_for, render_template
from flask_wtf import Form
from wtforms import StringField
from wtforms.validators import InputRequired


with open('iwlist/sep1.txt', 'r') as infile:

       data = infile.read()

lhs, rhs = data.split(":", 1)

ssid1 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep2.txt', 'r') as infile:

       data = infile.read()

lhs, rhs = data.split(":", 1)

ssid2 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep3.txt', 'r') as infile:

       data = infile.read()

lhs, rhs = data.split(":", 1)

ssid3 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep4.txt', 'r') as infile:

       data = infile.read()

lhs, rhs = data.split(":", 1)

ssid4 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep5.txt', 'r') as infile:

       data = infile.read()

lhs, rhs = data.split(":", 1)

ssid5 = ''.join(lhs.split())[:-2]



app = Flask(__name__)

class WifiForm(Form):
    password = StringField(validators=[InputRequired()])


@app.route('/', methods=['GET', 'POST'])
def wifi_password1():
    form = WifiForm(csrf_enabled=False)

    if form.validate_on_submit():
        password = form.password.data

        with open('wpa_supplicant.conf', 'w') as f:
            f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid1 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")

        return redirect(url_for('wifi_password1'))

    return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password2():
    form = WifiForm(csrf_enabled=False)

    if form.validate_on_submit():
        password = form.password.data

        with open('wpa_supplicant.conf', 'w') as f:
            f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid2 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")

        return redirect(url_for('wifi_password2'))

    return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password3():
    form = WifiForm(csrf_enabled=False)

    if form.validate_on_submit():
        password = form.password.data

        with open('wpa_supplicant.conf', 'w') as f:
            f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid3 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")

        return redirect(url_for('wifi_password3'))

    return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password4():
    form = WifiForm(csrf_enabled=False)

    if form.validate_on_submit():
        password = form.password.data

        with open('wpa_supplicant.conf', 'w') as f:
            f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid4 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")

        return redirect(url_for('wifi_password4'))

    return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password5():
    form = WifiForm(csrf_enabled=False)

    if form.validate_on_submit():
        password = form.password.data

        with open('wpa_supplicant.conf', 'w') as f:
            f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid5 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")

        return redirect(url_for('wifi_password5'))

    return render_template('main.html',form=form)




if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)

HTML:

<tr><td><h2 class="close"><a href="#one" data-transition="flip">
Quiana
</a>
<tr><td><h2 class="close"><a href="#two" data-transition="flip">
PaintItOrangeDucks
</a>
<tr><td><h2 class="close"><a href="#three" data-transition="flip">
Antimatter
</a>
<tr><td><h2 class="close"><a href="#four" data-transition="flip">
zaragoza1
</a>
<tr><td><h2 class="close"><a href="#five" data-transition="flip">
Palkimawar
</a>
</tbody>
</table>
</div>
  </div>
</div> 
<div data-role="page" data-dialog="true" id="one">
  <div data-role="header">
    <h1>
Quiana
</h1>
  </div>
  <div data-role="main" class="ui-content">
    <p>Please enter your Wi-Fi password.
<br><br>

<form method="POST">
        {{ form.hidden_tag() }}
        {{ form.password }}
        <input type="submit" value="CONNECT"/>
    </form>


    </div>
</div> 
</div>
<div data-role="page" data-dialog="true" id="two">
  <div data-role="header">
    <h1>
PaintItOrangeDucks
</h1>
  </div>
  <div data-role="main" class="ui-content">
    <p>Please enter your Wi-Fi password.
<br><br>

<form method="POST">
        {{ form.hidden_tag() }}
        {{ form.password }}
        <input type="submit" value="CONNECT"/>
    </form>


    </div>
</div> 
</div>

我需要知道如何为每个烧瓶分配自己的名称,所以当用户提交表单(post)时,它将使用正确的SSID编写文件...

我确定这是执行此表单的更好方法! 谢谢你的帮助。

0 个答案:

没有答案