0

First of all, apologize if this question may have been asked before. I am relatively new in web programming, especially using Flask and HTML. I've seen a similar question (with the answer) here, however, I still couldn't figure out how to get a checked checkbox from my HTML. Here is what I've done so far. So I have this html code:

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

The following is my 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)

When I print value, it returns None. Any suggestion or help would be really appreciated. Thank you

6
  • What is sync-button i can't see any thing related in your html content. Commented Jun 8, 2016 at 6:52
  • 1
    oh sorry, it is a button that will trigger my code to get the checked checkbox. i've updated the html. Thanks Commented Jun 8, 2016 at 7:00
  • Both are in different form i think that's the problem. Commented Jun 8, 2016 at 7:09
  • @shivsn i did. I got an empty list when I print value Commented Jun 8, 2016 at 7:46
  • @RahulKP hmm, I think you are right. Is there a way to get things right without put them in the same form? Commented Jun 8, 2016 at 7:52

1 Answer 1

0

Your checkbox and Button are in different forms. Make it in a single from. It will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.