Jinja2 Templates and Forms
Learn how to inject Python into HTML with templates and how to collect user data with forms.
StartKey Concepts
Review core concepts you need to learn to master this subject
Flask Templates
render_template
Function
Template Variables
Template Variable Filters
Template if
Statements
Template for
Loops
Template Inheritance
Flask Web Forms
Flask Templates
Flask Templates
Flask uses templates to expand the functionality of a web application while maintaining a simple and organized file structure. Templates are enabled using the Jinja2 template engine and allow data to be shared and processed before being turned in to content and sent back to the client.
- 1When you navigate through a website you may notice that many of the pages on the site have a similar look and feel. This aspect of a website can be achieved with the use of templates. In this les…
- 2Having routes return full web pages as strings is not a realistic way to build our site. Containing our HTML in files is the standard and more organized approach to structuring our web app. To wo…
- 3Instead of having an HTML file for each recipe, it would be a lot easier having one file for many recipes. Being able to pass data to template files is how we can begin to accomplish this goal. A…
- 4Now that we can use variables in our templates, let’s look at different ways we can perform actions on them. Filters are used by the template engine to act on template variables. To use them si…
- 5Including conditionals such as if and if/else statements in our templates allows us to control how data is handled. Let’s say we have a string variable passed to our template. When the variable …
- 7If you go to any website you may notice certain elements exist across different web pages. The navigation bar is a good example of a common page element. This is the banner at the top of most sit…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory
Flask Templates
Which directory does
render_template
look for template files?Flask Forms
Given the following 2 routes:
py @app.route("/items") def item_list(): return render_template("items.html") @app.route("/items/<int:id>) def get_item(id): return render_template("one_item.html", item_id=id)
Complete the code that setshref
to the URL string"/items/2"
.