Flask CRUD
About Lesson

In this Flask CRUD Application lesson we are going to talk about Flask CRUD Application Project Structure, we want to create our first project in flask, i have already installed flask, now open your favorite IDE, in my case iam using Pycharm IDE.

 

 

Installation

For this lesson first you need to install flask, and after that you need to install flask-sqlalchemy.

 

 

After installation create two folders, the first one is static where we want to add our static files like images, javascript and other files, the second one is templates folder where we want to add our html templates, for the bootstrap templates we are going to use Bootstrap CDN links, also create a Python file at name of App.py.

 

 

This is our Flask Project Structure for CRUD Application.

Flask Project Structure
Flask Project Structure

 

 

 

Open your App.py file and create your first Flask application using this code.

 

 

First of all Flask Applications must create an application object, The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). you can create the application object like this.

 

The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s __name__ variable is the correct value.

 

 

Also for every Flask Application we need to define routes, you can use app.route decorator for creating  routes in Flask, which registers the decorated function as a route. as we have already done in our above complete code like this, In this code we have registered the function index() as the handler for the applications’s root URL. functions like Index() are called view functions. a response returned by a view function can be a simple string with HTML content, but it can also take more complex forms.

 

 

 

At the end we need to run our web application using development server.

 

The __name__ == ‘__main__‘  is used here to ensure that the development web server is started only when the script is executed directly, and also you can see that we have made debugging mode True, but when you are going to publish your web application , you need to make false, for development purposes you can keep it true.

 

 

 

Now run your application, and go to http://localhost:5000/.  this will be the result.

Flask CRUD Application Project Structure
Flask CRUD Application Project Structure

 

 

 

 

OK now let’s add html file to our templates folder, iam going to name it index.html.

 

 

 

Now open your App.py file, instead of returning just simple string we are going to return a template.

 

 

 

Run your application and this is the result.

Flask CRUD Application Project Structure
Flask CRUD Application Project Structure

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found