0

On my page, I have 5 different text boxes, and I would like when the user submit button, the text-box with the data in it gets posted.

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>

I need to know how to assign each flask form its own name, so when the user submits the form (post) it will write the file with the proper SSID...

I'm sure theres a better way to execute this form! thank you for all your help.

  • Does this page actually render? – kylieCatt May 24 '15 at 03:19
  • yes, and it works too. the only thing not working is getting the different SSID's to post to the file. – Brayden DeVito May 24 '15 at 03:20
  • Well I can tell you that isn't going to work. When you post back to `/` how is Flask going to know which view method to use? – kylieCatt May 24 '15 at 03:26
  • 1
    So, what do you recommend in place of ' / ' I don't see how this is going to affect it. Enlighten me please! :} – Brayden DeVito May 24 '15 at 03:30
  • 1
    Why not use a `select` for the SSID? If each form needs to be unique, either you need to give each form a unique element (e.g., a hidden input) that identifies the form or you need to use a unique action for each form. – dirn May 24 '15 at 03:58
  • The argument passed to`app.route()` is the URL that assigned to that view method. So if you had a page to display users for example you might use `app.route('/users')`. If you have 5 methods that all have the same URL there is no way for Flask to know which one to use. – kylieCatt May 24 '15 at 04:06
  • @IanAuld Thank you for your advice. I'm a bit of a Python novice. I just wasn't sure which part on the flask bit to change to make it unique... because on the HTML it doesn't have a unique ID for each form that sticks out to me! How would the select function improve my script? – Brayden DeVito May 24 '15 at 04:07
  • I would suggest running through something like this to familiarize yourself with Python/Flask a little more: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world – kylieCatt May 24 '15 at 05:07
  • See [this answer](http://stackoverflow.com/questions/18290142/multiple-forms-in-a-single-page-using-flask-and-wtforms). – doru May 24 '15 at 07:47

0 Answers0