Seriously...what the flask?
After much confusion the day before, I'm starting to understand Flask a little bit better. There are some required syntax:
- Importing Flask and importing all the modules that is used in the server-side file.
- All of our assignments require us to have the app.run(debug=True) statement at the bottom on our server files so that if there were any issues with the code, the browser will give us error messages.
- app = Flask(__name__)
- app.secret_key = "your key", when you are passing POST info.
- app.route(): are where you direct where client-side action goes.
For example: <form action="/example"> will route to app.route('/example') and do whatever function you have written inside.
All app.route() must have a function inside it and that function must return something. Either a render_template('example.html') or a redirect('/example'). This is to ensure that whenever a button is pressed and the information is routed, there will be a page that exists. That last sentence probably doesn't make sense but that's the best that I can describe it.
We learned two main methods of passing information between client and server: POST and GET. Post is what you use during <input> elements and requires secret keys (see above) and is primarily used for private and sensitive information like passwords. GET is used to ask the server for information to display client-side.
- GET allows us to request information from client side webpages.
- We can use session['example'] = request.form('name') to store the GET request of the form element with the name 'name' (think <input type="text" name="first_name">).
And the last topic of the day is how to represent python code in your HTML. For just printing something that is stored in the server via session, just write {{session['example']}}. If you want to use if else, for loops, use {% algorithm %} and also {% endalgorithm %}. Ending the algorithm is very important!! Going through the assignments and getting a lot of help from my classmates solidified the foundation of Flask so I'm pretty proud of being able to create a small web apps.
No comments:
Post a Comment