Coder Social home page Coder Social logo

bew-1.2-forms-lab's Introduction

Lab - Books App Part 2 (Forms)

Why should I do this?

This lab will guide you through the process of writing and using Flask-WTForms form classes. This makes the process of using forms much more streamlined and "Pythonic", and will result in more robust, error-proof, and readable code. By the end of this lab, you should be ready to independently write and use form classes for your projects.

Setup

Clone this repository to your computer.

Take a look at the code - it looks a bit different than what you're used to. Namely, the code is now separated out into several files rather than being written in a single app.py file. Since we're now writing model and form code as well as route code, this will help us to maintain some structure and separation.

To run the code, navigate to the project folder and run the following to install the required packages:

pip3 install -r requirements.txt

Then, rename the .env.example file to .env:

mv .env.example .env

Then you can run the following to run the Flask server:

python3 app.py

Part 1: Explore Forms

Take a look at the books_app/forms.py file, which contains a form we'll use to collect user data on new books. The user can add a book's title, publish date, author, audience, and genres.

Take a look at the books_app/routes.py file, and see how the create_book route is using the BookForm class. Namely, we create an instance of BookForm and pass it to the template. Then when the form is submitted, we use the data to create a new Book object and save it to the database.

Now, run the server and test out the routes. See if you can create a few new books. Notably, you'll only be able to select authors that have already been added to the database - but don't worry, we'll be able to add new authors soon!

Part 2: Books, Authors, & Genres

Test out the + New Author and + New Genre links. It doesn't look like they're working yet! Modify the code to add a form to each route:

  • In forms.py, fill out the AuthorForm class with the appropriate fields. You may need to look up how to use the fields - you can use this guide as a reference.
  • In routes.py, complete the TODOs in the create_author route to instantiate an AuthorForm, send it to the template, and use it to create a new Author instance. You can use the create_book route as a reference.
  • In templates/create_author.html, use the form object to display its labels and fields. You can use the create_book.html template as a reference.

Then, test it out and see if you can create a new author! Once you're satisfied with your understanding, complete the same steps for Genre.

However, what if we want to modify a book (or author/genre) after it's created? We can use the same BookForm to do that, too! Complete the TODOs in the book_detail route to update the Book object once the form is submitted.

Part 3 (Stretch): Users

Now you're really getting the hang of using forms! If you've made it this far, here are some stretch challenges:

  1. Make a create_user route that uses a UserForm class to add new users to the database.
  2. In the profile route, allow a user to update their information, such as username and favorite books. (We don't have any authentication yet, so that means that anyone can update any user's profile.)
  3. Come up with your own ideas for new features and then implement them!

Resources

If you'd like more resources on working with Flask-WTForms, check out the following:

bew-1.2-forms-lab's People

Contributors

meredithcat avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

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.