首先,如果之前可能已经提出这个问题,请道歉。我在网络编程方面比较新,特别是使用Flask和HTML。我已经看到了一个类似的问题(答案)here,但是,我仍然无法弄清楚如何从我的HTML中获取一个选中的复选框。 这是我到目前为止所做的。所以我有这个HTML代码:
<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Prod To Mongo</label>
<div class="content">
<div class="left-content">
<h2 id="prod-left-h2">HOSTS</h2>
<form action="/" method="get">
<label><input type="checkbox" class="radio" name="mongodata" value="mongodata">mongodata</label>
<label><input type="checkbox" class="radio" name="host" value="1">assets-mongod</label>
<label><input type="checkbox" class="radio" name="host" value="1">memcached</label>
</form>
</div>
<div class="middle-content">
<h2 id="prod-left-h2">DBs</h2>
<label><input type="checkbox" class="radio" name="DBs" value="1">admin</label>
<label><input type="checkbox" class="radio" name="DBs" value="1">custsvc-mongod-</label>
</div>
<div class="right-content">
<h2 id=prod-left-h2>COLLECTIONS</h2>
{% for collection in collection_list %}
<label><input type="checkbox" class="radio" name="collection" value="1">{{ collection }}</label>
{% endfor %}
</div>
<div class="button-content">
<div class="get-collection-button">
<form action="/" method="post">
<button name="collection-button" value="get collections" type="submit" class="button"> get collections</button>
</form>
</div>
<div class="sync-data-button">
<form action="/" method="post">
<button name="sync-button" value="sync data" type="submit" class="button"> sync data</button>
</form>
</div>
</div>
</div>
</div>
以下是我的app.py:
from flask import Flask, render_template, send_from_directory,request
import os
from os.path import abspath, dirname
from django.shortcuts import render_to_response, RequestContext
from flask import send_from_directory
app = Flask(__name__,template_folder='ds-dashboard/templates/',static_folder='ds-dashboard/static')
# app = Flask(__name__,static_url_path='')
collections = ["A","B","C","D","E"]
@app.route('/')
def home():
return render_template('home.html')
@app.route('/',methods=['GET', 'POST'])
def button_clicked():
if request.form.get('sync-button',None) == "sync data":
value = request.form.getlist("mongodata")
print value
return "test"
elif request.form.get('collection-button',None) == "get collections":
return render_template('home.html', collection_list = collections)
if __name__ == '__main__':
app.run(debug=True, port=5001)
当我打印value
时,它会返回None
。任何建议或帮助将非常感激。谢谢
答案 0 :(得分:0)
您的checkbox
和Button
位于不同的forms
。将其设为一个from
。它会起作用。