We've spoken so much about this fancy form library - WTForms, let's see it in action and create a form using it in our Flask application.
1. Download the starting .zip files from this lessons resources.
2. Unzip and open the project in PyCharm. PyCharm may prompt you to create a new virtual environment and install the dependencies listed in the requirements.txt
. Agree and click OK.
This should do the trick. However, if you still see any red underlines in your main.py then tell PyCharm to check the virtual environment and dependencies again by going to File -> Reload All from Disk.
3. (Troubleshooting) If you don't get prompted set up a virtual environment, set one up manually by adding a new Python interpreter.
You can also find this under File -> Settings -> Project -> Python Interpreter. Click Add Interpreter -> Add Local Interpreter.
Leave the default settings and click OK
Do not tick "inherit global site-packages". When you click OK, you will create a new venv folder in your project. All of the project requirements and packages will be installed into this venv folder. This keeps the packages isolated from global settings and your operating system as well as all other projects. This is the ideal setup for all Python projects.
The requirements.txt file is a file where you can specify all the dependencies (the installed packages that your project depends on) and their versions. This means that you can share your project without all the installed packages, making it a lot more lightweight. When someone downloads your project (like you have done here), the requirements.txt file tells their code editor which packages need to be installed. Read more on this here.
To install a particular package you can use the Terminal. To install Flask-WTF you would use the pip install
command.
pip install Flask-WTF
*pip installs are always case sensitive!
You can install all the required packages listed in the requirements.txt file for the project at the same time:
On Windows type:
python -m pip install -r requirements.txt
On MacOS type:
pip3 install -r requirements.txt
Create the login route which renders the login.html
file.
Run the app to make sure it works. This is what you should see when you run it: