如何使用WTForms制作具有唯一ID的多个复选框,以便可以将其存储在货架上? - python

如何使用WTForms制作多个复选框并将其呈现到我的html页面?
这是我使用WTForms提出的内容,但有人告诉我使用Boolean(True,False)它将仅应用于一个复选框,而不是多个复选框。我如何将所选的复选框附加到字典中有唯一的ID吗?

我对python,flask和WTForms相当陌生。感谢您抽出宝贵的时间来帮助我。也可以给我一些建议/指南来帮助我进行工作。

A preview of my HTML doctor booking page

 

   {% extends 'layout.html' %}
    {% block body %}

 

    <link rel="stylesheet" type="text/css" href="/static/css/appointment.css">
    <div class="jumbotron">
      <div class="container text-center">
          <div id="head">
       <h1>{{ title }}</h1>
              </div>

        <div id="head-p"> <p> Doctor Opening Hours Slot  </p> </div>

      </div>
    </div>



      <div class="panel-group">
        <div class="panel panel-default">
          <div class="panel-heading"> <h3> Doctor Admin Page </h3> </div>
        <div id="appointment_box">


        </div>


        <form>
        <table class="table table-bordered">
            <thead>
            <tr>
                    <th> Monday  </th>
                    <td id="m1" style="background-color:lightgrey"> {{dform.active}} 8 : 00 </td>
                    <td id="m2" style="background-color:lightgrey"> {{dform.active}} 9 : 00</td>
                    <td id="m3" style="background-color:lightgrey"> {{dform.active}} 10 : 00 </td>
                    <td id="m4" style="background-color:lightgrey"> {{dform.active}} 11 : 00</td>
                    <td id="m5" style="background-color:lightgrey"> {{dform.active}} 12 : 00</td>
                    <td id="m6" style="background-color:lightgrey"> {{dform.active}} 13 : 00</td>
                    <td id="m7" style="background-color:lightgrey"> {{dform.active}} 14 : 00</td>
                    <td id="m8" style="background-color:lightgrey"> {{dform.active}} 15 : 00</td>
                    <td id="m9" style="background-color:lightgrey"> {{dform.active}} 16 : 00</td>
                    <td id="m10" style="background-color:lightgrey"> {{dform.active}} 17 : 00</td>
                    <td id="m12" style="background-color:lightgrey"> {{dform.active}} 18 : 00</td>
            </tr>

    <tr>
                      <th> Tuesday </th>
                    <td id="t1" style="background-color:lightgrey"> {{dform.active}} 8 : 00 </td>
                    <td id="t2" style="background-color:lightgrey"> {{dform.active}} 9 : 00</td>
                    <td id="t3" style="background-color:lightgrey"> {{dform.active}} 10 : 00 </td>
                    <td id="t4" style="background-color:lightgrey"> {{dform.active}} 11 : 00</td>
                    <td id="t5" style="background-color:lightgrey"> {{dform.active}} 12 : 00</td>
                    <td id="t6" style="background-color:lightgrey"> {{dform.active}} 13 : 00</td>
                    <td id="t7" style="background-color:lightgrey"> {{dform.active}} 14 : 00</td>
                    <td id="t8" style="background-color:lightgrey"> {{dform.active}} 15 : 00</td>
                    <td id="t9" style="background-color:lightgrey"> {{dform.active}} 16 : 00</td>
                    <td id="t10" style="background-color:lightgrey"> {{dform.active}} 17 : 00</td>
                    <td id="t11" style="background-color:lightgrey"> {{dform.active}} 18 : 00</td>
            </tr>

            <tr>
                              <th> Wednesday </th>
                    <td id="w1" style="background-color:lightgrey"> {{dform.active}} 8 : 00 </td>
                    <td id="w2" style="background-color:lightgrey"> {{dform.active}} 9 : 00</td>
                    <td id="w3" style="background-color:lightgrey"> {{dform.active}} 10 : 00 </td>
                    <td id="w4" style="background-color:lightgrey"> {{dform.active}} 11 : 00</td>
                    <td id="w5" style="background-color:lightgrey"> {{dform.active}} 12 : 00</td>
                    <td id="w6" style="background-color:lightgrey"> {{dform.active}} 13 : 00</td>
                    <td id="w7" style="background-color:lightgrey"> {{dform.active}} 14 : 00</td>
                    <td id="w8" style="background-color:lightgrey"> {{dform.active}} 15 : 00</td>
                    <td id="w9" style="background-color:lightgrey"> {{dform.active}} 16 : 00</td>
                    <td id="w10" style="background-color:lightgrey"> {{dform.active}} 17 : 00</td>
                    <td id="w11" style="background-color:lightgrey"> {{dform.active}} 18 : 00</td>
            </tr>
                    <tr>
                              <th> Thursday </th>
                    <td id="th1" style="background-color:lightgrey"> {{dform.active}} 8 : 00 </td>
                    <td id="th2" style="background-color:lightgrey"> {{dform.active}} 9 : 00</td>
                    <td id="th3" style="background-color:lightgrey"> {{dform.active}} 10 : 00 </td>
                    <td id="th4" style="background-color:lightgrey"> {{dform.active}} 11 : 00</td>
                    <td id="th5" style="background-color:lightgrey"> {{dform.active}} 12 : 00</td>
                    <td id="th6" style="background-color:lightgrey"> {{dform.active}} 13 : 00</td>
                    <td id="th7" style="background-color:lightgrey"> {{dform.active}} 14 : 00</td>
                    <td id="th8" style="background-color:lightgrey"> {{dform.active}} 15 : 00</td>
                    <td id="th9" style="background-color:lightgrey"> {{dform.active}} 16 : 00</td>
                    <td id="th10" style="background-color:lightgrey"> {{dform.active}} 17 : 00</td>
                    <td id="th11" style="background-color:lightgrey"> {{dform.active}} 18 : 00</td>
            </tr>


                    <tr>
                              <th> Friday </th>
                    <td id="f1"  style="background-color:lightgrey"> {{dform.active}} 8 : 00 </td>
                    <td id="f2"  style="background-color:lightgrey"> {{dform.active}} 9 : 00</td>
                    <td id="f3"  style="background-color:lightgrey"> {{dform.active}} 10 : 00 </td>
                    <td id="f4"  style="background-color:lightgrey"> {{dform.active}} 11 : 00</td>
                    <td id="f5"  style="background-color:lightgrey"> {{dform.active}} 12 : 00</td>
                    <td id="f6"  style="background-color:lightgrey"> {{dform.active}} 13 : 00</td>
                    <td id="f7"  style="background-color:lightgrey"> {{dform.active}} 14 : 00</td>
                    <td id="f8"  style="background-color:lightgrey"> {{dform.active}} 15 : 00</td>
                    <td id="f9"  style="background-color:lightgrey"> {{dform.active}} 16 : 00</td>
                    <td id="f10" style="background-color:lightgrey"> {{dform.active}} 17 : 00</td>
                    <td id="f11" style="background-color:lightgrey"> {{dform.active}} 18 : 00</td>
            </tr>

                    <tr>
                              <th> Saturday </th>
                    <td id="s1" style="background-color:lightgrey"> {{dform.active}} 8 : 00 </td>
                    <td id="s2" style="background-color:lightgrey"> {{dform.active}} 9 : 00</td>
                    <td id="s3" style="background-color:lightgrey"> {{dform.active}} 10 : 00 </td>
                    <td id="s4" style="background-color:lightgrey"> {{dform.active}} 11 : 00</td>
                    <td id="s5" style="background-color:lightgrey"> {{dform.active}} 12 : 00</td>
                    <td id="s6" style="background-color:lightgrey"> {{dform.active}} 13 : 00</td>
                    <td id="s7" style="background-color:lightgrey"> {{dform.active}} 14 : 00</td>
                    <td id="s8" style="background-color:lightgrey"> {{dform.active}} 15 : 00</td>
                    <td id="s9" style="background-color:lightgrey"> {{dform.active}} 16 : 00</td>
                    <td id="s10" style="background-color:lightgrey"> {{dform.active}} 17 : 00</td>
                    <td id="s11" style="background-color:lightgrey"> {{dform.active}} 18 : 00</td>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
      <br>
             <div class="form-group">
                <div class="form-row">
                    <div class="form-group col-md-11">


                    </div>
                    <div class="form-group col-md-1">
                        <input type="submit" value="Submit" class="btn btn-info"/>
                    </div>
                </div>
            </div>

        </form>

        </div>
      </div>

    {% endblock %}

from flask import Flask, render_template, request, flash, redirect, url_for, session
import shelve
# <<<<<<< HEAD
from wtforms import Form, StringField, BooleanField,TextAreaField,RadioField, SelectField, validators, PasswordField, DateField, SubmitField, \
 IntegerField, FileField
from wtforms.validators import DataRequired
from datetime import date
import uuid
from wtforms import widgets, SelectMultipleField

app = Flask(__name__)

#this is my form!
 class DoctorAppointment(Form):
   active = BooleanField(True, False)
   
   
@app.route("/doctorbooking")
def doctorbooking():
    doctor_form = DoctorAppointment(request.form)
    if request.method == 'POST' and form.validate():
        check_list = doctor_form.active.data
        doctorappointment = Timeslots(day,check_list)

        db_read = shelve.open("doctorappointment.db")

        try:
            doctorappointmentlist = db_read["doctorappointment"]

        except:
            doctorappointmentlist = {}

        doctorappointmentlist[day] = check_list.data


        db_read.close()


        flash("Appointment added sucessfully!")
        
          return render_template("DoctorBooking.html", title="Doctor Booking",dform = doctor_form)

参考方案

请告诉我是否有帮助。最后,将返回一个字典(数据)以及选中的复选框的名称。

烧瓶代码:

@app.route('/index/', methods=['POST', 'GET'])
def index():
    weekdays = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
    timeslots = ['8AM', '9AM', '10AM', '11AM', '12PM', '13PM', '14PM','15PM','16PM','17PM','18PM']

    if request.method == "POST":
        data = dict((key, request.form.getlist(key) if len(
            request.form.getlist(key)) > 1 else request.form.getlist(key)[0])
            for key in request.form.keys())
        print (data) # do whatever you want with this data dict. Store it, flash messages, etc.

    return render_template('index.html',
       weekdays = weekdays,
       timeslots = timeslots,
       )

模板

<form method = "post" action = "">
  <table class="table table-bordered">
    <thead>
      {% for weekday in weekdays %}
      <tr>
        <th> {{ weekday }} </th>
          {% for timeslot in timeslots %}
            <td><input id="{{ weekday+timeslot }}" type="checkbox" name="{{ weekday+timeslot }}">{{ timeslot }}</td>
          {% endfor %}
      </tr>
      {% endfor %}
    </thead>
  </table>
  <div class="form-group col-md-1">
    <input type="submit" value="Submit" class="btn btn-info"/>
  </div>
</form>

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

将python scikit学习模型导出到pmml - python

我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

通过电子邮件发送库存状态表 - javascript

                        我正在寻找一种通过电子邮件发送具有相同HTML结果的产品确认页(带有可用输入)的解决方案。我对从mail.php的确认页面获取输入不感到困惑。请在下面找到代码。谢谢stockdetailform.php<html> <head> <title>Stockr Form</…