GithubHelp home page GithubHelp logo

dynamic-flask-form's Introduction

Dynamic Flask Form

Having struggled to find a workind example of a WTForm in Flask with a dynamic form, I created this one. It is cobbled together from a few Stackoverflow and Google Group discussions.

I'm a total noob so if you have suggestions I'd be glad to include them!

What does it do?

  • Render a form with two models
  • Save and load the related models

This means you can have a form with dynamically created fields which are saved and loaded properly.

Usage

You can use requirements.txt to install the packages, preferably in a virtualenv. Then you can simply run multimodel.py. The default port is set to 5002.

Basic Concepts

  • FieldList and FormField are used to nest one Form inside another
  • phones is a relation in the User Model creating a link to Phone entries.
  • PhoneForm deals with the Phone fields only
  • CombinedForm includes the User and Phone fields
  • The CombinedForm is populated through the User model and related phones

Pain Points

User Model

I had named the phones relation phone for a while which broke the populate_obj function. The naming must match for the mechanism to work. Duh.

PhoneForm

This is the form that is nested in the 'main' Form. It is not directly exposed or rendered. A common problem here is the CSRF token being missing from this form. Options to solve this problem are explained here. I chose to subclass wtforms Form which does not require the token.

CombinedForm

phones = FieldList(FormField(PhoneForm, default=lambda: Phone()))

That is the line of code where it all broke down for me. Once there is a working example it's hopefully not so mysterious. Setting the default to lambda: Phone() was difficult for me to get to (see Fixing populate_obj)

Index

I just load the one user from the db, no messing around. Playing with a dropdown for different users just distracts from the main point here.

I'm not sure if aassing a new Phone instance where the User has no phones is the best possible solution but it meant I could keep using the JavaScript Code as it was.

Javascript

I simply fixed example code that I found. There is probably a neater way of copying the fields that I haven't found. I would gladly take suggestions in this case as well as the rest of the code.

Resources

Half working example Fixing populate_obj Error display in View FormField Documenation

dynamic-flask-form's People

Contributors

sebkouba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dynamic-flask-form's Issues

Need for zero items support

Hi, In a project I'm working on I need the option of having zero items in the telephone list.
From what I understand this is not possible with your code?
Any idea what changes must be implemented to support this feature? I have no .js experience at all.
Tried the fork from rienafairefr: [https://github.com/rienafairefr/dynamic-flask-form] but cant get it to work.
Besides my zero row issue, your code is working perfectly!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.