Latest Updates

CURD Operations using Flask and MongoDB



What is Web Framework?

Web Application Framework or simply Web Framework represents a collection of libraries and modules that enables a web application developer to write applications without having to bother about low-level details such as protocols, thread management, etc.

FLASK

Flask is a web application framework written in Python. Armin Ronacher, who leads an international group of Python enthusiasts named Pocco, develops it. Flask is based on Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.

MongoDB

you can read my full blog post for MongoDB:

https://all-about-devops.blogspot.com/2021/05/ea-case-study.html

WSGI

Web Server Gateway Interface (WSGI) has been adopted as a standard for Python web application development. WSGI is a specification for a universal interface between the web server and the web applications.

Werkzeug

It is a WSGI toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it. The Flask framework uses Werkzeug as one of its bases.

Jinja2

Jinja2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.

CURD Application

  • Create -  Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.
  • Update - Update operations modify existing documents in a collection. 
  • Read - Read operations retrieve documents from a collection that means to query a collection for documents.
  • Delete - Delete operations remove documents from a collection. 

so for each operation I have created different routes and using these routs I managing all these operations so let's see one by one

1. Connectivity: 

So for working with flask, we need a flask package and for connecting with MongoDB we need PyMongo Package, and using the "pip" command you can install these packages.


2. Create or Insert:

Here I have created 2 routes 

1. "/" - This route gives you an index page of the application and it has one form.

2. " /insert " - This route connected to a form whenever a form submit that was on the index page it will send data using a POST request and we will insert that data using MongoDB command insert_one() function we will insert it into the database and you know MongoDB mostly work on JSON so in JSON syntax we need to insert. so on successful insert, it redirects us to table so there we can see our data that was stored now in the database.


3. View or Read:

To show all the data I have created this "/show " route it will run on clicking the view button on the index page so using MongoDB find() function we will retrieve all documents in the student variable and we will pass this data in an HTML page and using Jinja for loop we will retrieve each document and put in the table.

Jinja loop:
{% for i in  array %}
   {{  i['your attribute'] }}
{% endfor %}


4. Update:

For update, I used 2 route

1. "/update/<ObjectId: id>" -  In this route, I passed objectid dynamically so whenever you click on the update button that particular id will go and retrieve all attributes and will give you a form where you update the data.

2. "/update_record/<ObjectId: id>" -  In this route after submitting the update form this route will take all data with objectid using the POST method and after retrieving data using MongoDB update_one() command we will Update this data again we need to follow JSON syntax and we filtered data using objectid. After the successful update we will redirect to show table.



5. Delete:

For removing data we are using the "/delete/<ObjectId: id>" route so according to a particular row it will take object id and using this id and MongoDB delete_one() the function we will remove that particular document and on successful we will be redirected to our data table.




GitHub Link:

https://github.com/venkateshpensalwar/Python-Flask-applications

Tutorial:


Conclusion:

In this way, we can create a CURD app, and we have seen that it is very easy to code we just used some routes and several lines of code so using some more logic you can also create an amazing application using FLASK and MongoDB.

Hope This Blog helpful to you!!!


No comments