🔧 PUT, PATCH & DELETE with Flask – What I Learned in MERN Stack Session 11
In our 11th MERN Stack session, I explored the real-world use of different HTTP methods (PUT, PATCH, DELETE) and how backend applications are structured using the MVC/MTV architecture. What made this session truly valuable was the hands-on practice using Flask, a lightweight Python web framework.
Let me walk you through what I learned — in a simple and clear way, using the actual code we implemented.
🧹 DELETE – Removing a Record
We used this route to delete a student record by index:
@app.route('/student/delete/<index>', methods=['DELETE'])
def student_delete(index):
del studentDB[int(index)]
return "Record deleted.."
🔹 When we call this endpoint with a student index, it removes that entry from the database. 🔹 A good example of how RESTful APIs handle deletion using the HTTP DELETE method.
🔄 PATCH – Updating Part of a Record
Here's the code to update just one field of a student record:
@app.route('/student/update/<item>/<index>', methods=['PATCH'])
def studentpatch(item, index):
data = request.json
if item in data:
studentDB[int(index)][item] = data[item]
return f"{item} record patched... {index}"
else:
return "Key does not exist.."
🟨 PATCH is used when you want to update only one specific value — like just a student's name or age — without touching the rest of the record. 🛠️ In our case, we check if the key exists and then apply the change.
🔁 PUT – Replacing the Whole Record
We also worked with PUT, which replaces the full data of a record:
@app.route('/student/update/<index>', methods=['PUT'])
def studentput(index):
data = request.json
if index in studentDB:
studentDB[int(index)] = data
return "Entire record updated.."
else:
return f"{index} ID does not exist.."
🟩 PUT is like saying: “Here’s a brand-new record — replace the old one completely.” We send the full data, and it overwrites the existing record if the ID is found.
🏗️ MVC vs MTV – Structuring Code the Smart Way
In addition to learning about HTTP methods, we also discussed two common design patterns used in backend applications:
🔷 MVC (Model-View-Controller) – Common in Node.js (Express):
🔶 MTV (Model-Template-View) – Seen in Django:
📌 Final Thoughts
🔹 I now clearly understand the differences between PUT and PATCH, and when to use each. 🔹 Implementing them using Flask gave me practical, hands-on experience.
🔹 The session helped me become a better backend developer by learning to organize logic using MVC/MTV patterns.
If you’re learning full-stack development or working with APIs, I hope this article made it a bit easier to understand how real backend systems work!
Let’s connect if you’d like to share knowledge, projects, or opportunities! 🚀
#Flask #Python #BackendDevelopment #RESTAPI #PUTvsPATCH #MERNStack #FullStack #MVC #MTV #WebDevelopment #LearningByDoing