Logo

dev-resources.site

for different kinds of informations.

Flask: The basics!

Published at
6/25/2024
Categories
python
flask
softwareengineering
students
Author
trippl
Author
6 person written this
trippl
open
Flask: The basics!

Welcome back SE nerds!! I'm back with another blog to talk about everything you'll need to know about Python Flask! Flask is an awesome addition to being a full-stack developer, bringing the backend dynamics and the next step to put the frontend and backend together!

This first thing you should be familiar with is an extension called Flask-SQLAlchemy (pronounced 'sequel alchemy'). This extension is a great Object-Relational Mapping tool that allows you to work with databases! An example of code that uses SQLAlchemy, will look like this:

Image description

Another good thing to know is about relationships. These can be one-to-many or many-to-many relationships between tables that are setup using the 'db.relationship' method. A one-to-many relationship is a single record linked to multiple records. This is what that code would look like:

class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
user = db.relationship('User', backref=db.backref('posts', lazy=True))

Many-to-many relationships means the records in one table can be linked to multiple records in another table, and vice versa. Code for this will look something like this:

Image description

A really cool feature to use with Flask is the use of APIs. Flask can handle APIs in various formats, including JSON. This is what a code for this will look like:

Image description

REST or Representational State Transfer is also good to know for Flask. This is an architectural style that uses HTTP requests to access and use data. Flask routes can handle RESTful operations defined by HTTP methods like GET, POST, PUT, DELETE. This code will look like This:

Image description

Constraints and Validations are key to having a legitimate backend. These ensure data integrity and correctness. In FLask-SQLAlchemy, these can be applied directly at the model level as shown in this example:

Image description

Cookies are also a good thing to know about. These are small pieces of data stored in the user's browser. Flask reads cookies easily. Here's an example of code using Cookies:

Image description

Authentication is also a good tool to know. This involves identifying users and allowing them access to certain resources. This is an example of code using authorization:

Image description

Password protection as you can guess, is an important tool when using personal data for a user. Passwords should be securely stored by hashing them before they're saved to the database. Flask uses libraries such as Flask-Bcrypt for this. Here's an example of how password protection works:

Image description

I hope you found this helpful, and if there are any errors or you would like to comment, feel free!!

students Article's
30 articles in total
Favicon
Cรณmo puedes llegar a ser AWS Cloud Club Captain (consejos de una capitana!)
Favicon
Understanding the Cyber Olympiad Levels: Beginner to Advanced
Favicon
The easiest hack to building projects as a student
Favicon
Preparing for the International Math Olympiad: Tips from Past Medalists
Favicon
The Evolution of the international mathematics Olympiad: A Historical Perspective
Favicon
The easiest way to build projects as a student
Favicon
Best Fintech Apps for Students: Taking Control of Your Finances
Favicon
Synergy 2024 - Delhi NCRโ€™s Biggest Tech-Fest | SGT University
Favicon
How to Approach Recursive Functions in Your Coding Assignments
Favicon
Title: Online vs. Offline Degrees: Understanding the Differences
Favicon
Join the AWS Generative AI Festival for a Chance to Win a Box of Swag!
Favicon
Does American Airlines Offer a Student Discount?
Favicon
The Biggest Difficulties Students Face When Deciding to Study Abroad
Favicon
Find Your Successful Path: Free Ikigai Template For Students!
Favicon
Innovations in Education: Evolving SOELs with ATELIER STUDIOS
Favicon
Flask: The basics!
Favicon
Top Websites Offering Student Discounts on Essential Developer Tools
Favicon
Building Student Advisor using Lyzr SDK
Favicon
Guest Lecture for Delta-N at Haagse Hogeschool in Den Haag
Favicon
15+ Free Writing tools for Students and Writers
Favicon
The Problem with Learning Styles and How to Use It to Your Benefit
Favicon
Organize Your Digital Study Materials Quickly Using This Tool
Favicon
Productivity Hacks for Students and Working Professionals: How to Stay Focused and Accomplish More
Favicon
Well...it's been a minute!
Favicon
All you need to know about AWS Cloud Club
Favicon
How to plan Hackathon for Undergrad Students
Favicon
Top 5 Student Ambassador Programs.
Favicon
Monsters the Book, Students Edition
Favicon
GitHub student developer pack?
Favicon
How To Find The Best Law Homework Help?

Featured ones: