This shows you the differences between two versions of the page.
ii:labs:s2:02:tasks:04 [2024/03/17 15:45] florin.stancu removed |
ii:labs:s2:02:tasks:04 [2024/03/17 18:43] (current) florin.stancu [04. [10p] Serving dynamic content] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 04. [20p] Advanced: File Database ==== | + | ==== 04. [10p] Serving dynamic content ==== |
- | As a final task, we want to store some of the user's personal data into a simple file-based "database" on the server. | + | Finally, we shall see how dynamic content can be interpolated |
- | + | ||
- | * Check the ''account-details.html'' template page for any TODOs there! | + | |
- | * Ultimately, the form inputs should be pre-filled with the values stores inside the database. | + | |
- | * Implement the ''/account-details'' route handler to read / write to the ''database.txt'' file on the server; | + | |
- | * You will have to support both GET and POST, so make sure to edit the decorator's arguments! | + | |
+ | * Edit the website's second HTML page, enter ''%%{{mycontent}}%%'' (this literal text!) as content somewhere inside the main content block: <code html> | ||
+ | <div class="content-box main-content"> | ||
+ | ... | ||
+ | {{mycontent}} | ||
+ | ... | ||
+ | </code> | ||
+ | * Now modify the ''server.py'' code (the ''second_page'' function) to take in the desired value as a request parameter and pass it to the ''mycontent'' template variable, e.g.: <code python> | ||
+ | def second_page(): | ||
+ | render_template("second.html", mycontent=request.args.get("mycontent", "<not specified>")) | ||
+ | </code> | ||
+ | * Open the endpoint URL and pass the ''mycontent'' value as a ''GET'' //URL parameter// (e.g., ''?parameter=value'') and see if the content changes dynamically after the request! | ||
+ | * This is a template engine called Jinja, seamlessly integrated with Flask. More about this: next time! | ||