mercredi 21 décembre 2016

Import checkbox value from database

I'm developing a Python-Flask Web application that should work as a wikipedia for politicians. When I create a new politician it is ticked as a draft by default that can be accepted by a higher ranked member. Therefore, when I create the politician page it is checked as draft and when the admin accepts that draft, the draft value goes to false. I'd like to know how do I tick/untick a checkbox based on the column value of the attribute draft, which is a boolean (True/False). Then I want to change it based on my form result. This is an edit function as you'll see:

This is my Politician Form, the one I use to edit:

class PoliticForm(FlaskForm):
  publicName = StringField('Public Name', validators=[DataRequired("Please enter politician public name.")])
  completeName = StringField('Complete Name', validators=[DataRequired("Please enter politician complete name.")])
  startDate = DateField('Start Date', format='%m-%d-%Y', validators=[DataRequired("Please enter the politician start Date.")])
  endDate = DateField('End Date', format='%m-%d-%Y', validators=(validators.Optional(),))
  draft = RadioField('Draft', choices=[(True,'Yes'),(False,'No')], )
  submit = SubmitField('Add Politician', validators=(validators.Optional(),))

And I want to check that draft row on my HTML Jinja2 view, which is:

<div class="form-group">
                        <label for="draft">Draft</label>
                        <input type="checkbox" name="draft" class="form-control" id="email" value="">
                    </div>

Do I need to used the checked paramater? How do I tick the check it it's True and uncheck if its False and then send that change to my controller?

politicians/http://ift.tt/2hVf6I8

@politicians_blueprint.route("/edit_politician/<idPol>", methods=["POST", "GET"])
@login_required
def edit_politician(idPol=1):
  form = PoliticForm()
  politician = Politic.query.filter_by(idPolitician=idPol).first()
  if request.method == "POST":
    politician.publicName = request.form['publicName']
    politician.completeName = request.form['completeName']
    politician.draft = request.form['draft']
    if request.form.get('date') != 'None':
      politician.startDate = datetime.datetime.strptime(request.form.get('date'), '%m/%d/%Y').strftime('%Y-%m-%d')
      print politician.startDate
    if request.form.get('date2') != 'None':
      politician.endDate = datetime.datetime.strptime(request.form.get('date2'), '%m/%d/%Y').strftime('%Y-%m-%d')
      print politician.endDate
    db.session.commit()
    return redirect(url_for('home.home'))
  elif request.method == "GET":
    print politician.startDate
    if politician.startDate is not None:
      dateStart = politician.startDate.strftime("%Y-%m-%d")
      politician.startDate = datetime.datetime.strptime(dateStart, '%Y-%m-%d').strftime('%m/%d/%Y')
    if politician.endDate is not None:
      dateEnd = politician.endDate.strftime("%Y-%m-%d")
      politician.endDate = datetime.datetime.strptime(dateEnd, '%Y-%m-%d').strftime('%m/%d/%Y')

    return render_template("editPolitician.html", politician=politician)

Any ideas? I'm really struggling with this..

Regards




Aucun commentaire:

Enregistrer un commentaire