0

HTML file (+Flask+Bootstrap)

{% extends "bootstrap/base.html" %}
{% block content %}

<html>
<head>
<style>
    table, th, td {
        background: #f5f5f5;
        border-collapse: separate;
        box-shadow: inset 0 1px 0 #fff;
        font-size: 12px;
        line-height: 24px;
        margin: 30px auto;
        text-align: left;
        width: 800px;

    }
    .center {
        text-align: center;
        margin: auto;
        width: 80%;
        border: 3px solid rgba(0, 0, 0, 0);
        padding: 20px;
    }
    .footer {
        position: absolute;
        right: 0;
        bottom: 0;
        left: 0;
        padding: 1rem;
        background-color: #efefef00;
        text-align: center;
    }
</style>
</head>
<body>

    <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <ul class="nav navbar-nav">
                <a class="navbar-brand"> TMS </a>
                <li><a href="/routing">Routing</a></li>
                </ul>
            </div>  
    </nav>
<table style="width:auto" align="center">
<tr>
<th>
<p>Select the route sheet:</p>
<div class='center'>
<form ip="upload-form" action="{{ url_for('routing') }}" method="POST" 
 enctype="multipart/form-data">
        <input type="file" name="file" accept="/" multiple>
        <input type="submit" action="submit" value="send">
    </div>
</th>
</tr>
</table>

{% if ifHTMLConditional %}

<form method='POST' action="{{ url_for('routing') }}">
<table>
<tr>
    <th>Name</th>
    <th>Meters</th>
</tr>
{% for i in returnFromObjectClassRoute %}
<tr>
<td>
    <input type="checkbox" name="vehicle" value= {{ i[1] }}>
    {{ i[1] }}
    </input>
</td>
<td>
    {{ i[2] }}
</td>
</tr>
{% endfor %}
</table>
<input type="submit" value = {{ test.test }}>
{% endif %}

</form>
</body>

</html>
{% endblock %}

How could I use this for loop to create the number of checkboxes but with returns for using them in next functionalities.

The goal of the program is to create a Short Route. With the checkbox I want to let the user to select where is the startPoint for the algorithm

Python Code

@app.route('/routing', methods=['GET', 'POST'])
def routing():
test = Reference()
print(test.test.data)
if test == True:
    return redirect('/home')
else:   
    if request.method == 'POST':
        directory = os.path.join(APP_ROOT)
        ifHTMLConditional = False
        if not os.path.isdir(directory):
            os.mkdir(directory)
        for file in request.files.getlist("file"):
            filename = file.filename
            destination = "/".join([directory, filename])
            if file.filename != '':
                file.save(destination)
                objectFromClassRoute = Route(filename, 'totalDistances_AB_BA_12012018.csv')
                returnFromObjectClassRoute = objectFromClassRoute.routing1()
                ifHTMLConditional = True
            else:
                returnFromObjectClassRoute = []
                ifHTMLConditional = False
                return redirect ('/home')
            return render_template('routing.html', test=test, returnFromObjectClassRoute=returnFromObjectClassRoute, ifHTMLConditional=ifHTMLConditional) 
return render_template('routing.html')

returnFromObjectClassRoute returns a list as a value.

enter image description here

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • code looks right, whats the problem? do you want to send the selected `checkbox` to the server to decide on it? then you need js code to send `ajax` requests. – senaps Jan 30 '18 at 06:32
  • Yes, I would like to recieve which checkbox It´s selected, but because I have a for loop I just have one Checkbox tag and it means I cannot get the value of each one of them right?. I'm going to check tha ajax from js and I'll let you know what I found and if it works. Thanks – Luis Alfonso Cano Jan 30 '18 at 12:32
  • I found how to do what I was looking for: I let an example here: – Luis Alfonso Cano Jan 30 '18 at 23:09

1 Answers1

0

Python code

An example for what I was looking for:

from flask import Flask, render_template, request

app = Flask(__name__)
app.config['SECRET_KEY'] = 'ThisIsSecret'

class Reference():
     def testList(self):
         testList = ['ABC', 'DEF', 'GHI']
         return testList

 @app.route('/home', methods=['GET', 'POST'])
 def home():
     test1 = Reference()
     test = test1.testList()
     print(test1.testList())
     if request.method == "POST":
         selected_contacts = request.form.getlist("test")
     return render_template('checkBoxTest.html', test=test)

 if __name__ == '__main__':
     app.run(debug=True)

Html + Jinja2 code

<html>
     <head>
     </head>
    <body>
    <form method='POST' action="{{ url_for('home') }}">
         {{ test.csrf_token }}
        {% for i in test %}
        <input type="checkbox" name="test" value="{{i}}">
        <label>{{i}}</label>
        {% endfor %}
    <input type="submit" action="submit">
    </form>
    </body>
</html>

It returns all the check list you had activate before submitting and return a list of the values where you can used them as you want