Obvious webdevel things dump

Having just spent hours debugging simple Flask application, I need to went:

1. tzdata package in fedora-minimal container image is broken. RPM database lists files which are not installed (actually, they were removed during base container build). It's getting fixed.

Symptom: FileNotFoundError: [Errno 2] No such file or directory: '/usr/share/zoneinfo/zone.tab'

Workaround: microdnf reinstall tzdata.

2. HTML checkboxes are weird. When they are set, they appear in HTTP request data with value. When unset, they are not in the request (i.e. one cannot look for checked = False). Thus, following is enough to find if the checkbox has been checked:

if "checkbox_name" in request.form:

3. default-on checkboxes with state retained over POST… This one was tricky. Apparently, initialising Flask-WTForms with request data on the first load overwrites default state. Solution: use form data in POST handler only.

class SomeForm(FlaskForm):
  checkbox_name = BooleanField("Some label", default=True)

  @app.route("/", methods=["GET", "POST"])
  def main():
      # doing "form = SomeForm(request.form)" here would obliterate default checkbox state; don't do it

      if request.method == "POST":
          form = SomeForm(request.form)
          # not a POST? start with default, empty form
          form = SomeForm()


Comments powered by Disqus