/MVC-in-Python

An MVC Python framework

Primary LanguagePythonThe UnlicenseUnlicense

MVC in Python

This project aims to provide an MVC Python framework for you to use in your projects. If you want to see an example of this framework there is a simple and complete example about a database manager. I believe that this example will make understanding easier of how the mechanics of MVC works in Python, in addition to give you a general view of how to make a project with this pattern.


What is MVC?

Briefly, MVC (Model View Controller) is a design pattern widely used in projects because it leaves the project structured in order to facilitate the identification of application modules, understand how it is structured, in addition to facilitating maintenance. It structures the project in three modules:

Name Funcion
Models Responsible for business logic
View Responsible for the visual part
Controllers Responsible for the behavior of the visual part

How to use this structure in my project?

The core directory contains all classes responsible for MVC operations in Python. If you want to use this structure in your project, you need to know how to use the two classes contained in the core directory: Core (responsible for opening controllers) and Controller (responsible for opening views).

Opening controllers

Core class has a method called "openController", which takes the controller name as a parameter (without including "Controller" in its name). It is crucial that when creating your controllers you follow the following nomenclature:

  • File name: <NameController>Controller.py
  • Class name: NameController

Note that the controller name must starts with a capital letter

Ex - Open src/controllers/HomeController.py
  from core.Core import Core
 
  home = Core.openController("home")
 

All application controllers must extend the "Controller" class, located in src/core/Controller.py. To open a view inside a controller, just call "loadView" method passing the view name as a parameter.

Ex - Open src/views/AddView.py
  from core.Controller import Controller
 
  class AddController(Controller):
    def __init__(self):
        self.addView = self.loadView("add")
 

Views creation

For creating views you must use the following naming pattern:

  • File name: <NameView>View.py
  • Class name: NameView Furthermore, all view classes must extend View class.

Note that the view name must starts with a capital letter

Export for personal project

The contents of the src folder are all you need to apply this pattern to your project.

Overview of a generic MVC Python framework structure

Project organization

The project is in src folder. In it, there are four folders and two files.

/src

Name Type Function
controllers Directory Contains all application controller classes
core Directory Contains the classes responsable for the MVC operations in Python
models Directory Contains all application model classes
views Directory Contains all application view classes
config.py File MVC configuration file (it is important for Core class)
Main.py File File responsible for the application start

/tests

Name Type Function
core Directory Contains all tests related to the Core class

Example

A simple and complete example of an application using this framework can be obtained from the example folder.