来自python / flask循环的bootstrap复选框

时间:2018-01-30 00:14:53

标签: python flask flask-bootstrap

HTML文件(+ 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 %}

我怎样才能使用这个for循环来创建复选框的数量,但是在下一个功能中使用它们的返回值。

该计划的目标是创建一条短路线。使用复选框,我想让用户选择算法的startPoint在哪里

Python代码

@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将列表作为值返回。

enter image description here

1 个答案:

答案 0 :(得分:0)

Python代码

我正在寻找的一个例子:

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代码

<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>

它会在提交之前返回您激活的所有检查清单,并返回您可以根据需要使用它们的值列表