Let’s see what MVC stands for :
- Modal : Represents the data and logic of the application
- View : The user interface that displays the data
- Controller : Handles user input and updates the model and view accordingly
Creating a custom MVC framework can be a rewarding experience, but it’s also a significant undertaking. It requires a deep understanding of design patterns , programming principles , and the specific requirements of your application.
Key Components and Structure
- Request Handling :
- A central dispatcher or router to intercept incoming requests
- Mapping of URLs to specific controllers and actions
- Model :
- Classes representing data entities
- Methods for retrieving , creating , updating , and deleting data
- Consider using database or other data storage mechanism
- View :
- Templates or files that define the UI (e.g., HTML etc.)
- Placeholders for dynamic content to be filled by the controller
- Techniques like templating engines
- Controller:
- Handles user requests and interacts with the model and view
- Receives input , processes it , and updates the model as needed
- Renders the appropriate view with the updated data
from flask import Flask, render_template
app = Flask(__name__)
class User:
def __init__(self , id , name ):
self.id = id
self.name = name
@app.route('/')
def index():
users = [User(1, "Neha"),User (2, "Rohan")]
return render_template('index.html' , users=users)
if __name__ == '__main__':
app.run()