The Django framework MVC is actually very simple
Let's look at a simple example where you can tell how the functionality of the web framework is different from the way it was before. Here's an example of how to do this by using Django: First, we split into 4 python files (models.py, views.py, urls.py) and HTML template files (latest_books.html).
models.py:
# models.py (the database tables) from django.db import Modelsclass book (models. Model): name = models. Charfield (max_length=50) pub_date = models. Datefield ()
views.py (The business logic)
# views.py (The business logic) from django.shortcuts import render_to_responsefrom models import bookdef latest_books (req uest): book_list = Book.objects.order_by ('-pub_date ') [: ten] return render_to_response (' latest_books.html ', {' Book_list ': book_list})
urls.py (the URL configuration)
# urls.py (the URL configuration) from django.conf.urls.defaults import *import viewsurlpatterns = Patterns (", (R ' ^ latest/$ ', views.latest_books),)
Latest_books.html (the template)
# latest_books.html (the template)
Do not care about grammatical details, as long as the heart feel the overall design. Here are just a few files to focus on:
- The models.py file mainly uses a Python class to describe the data table. Called model. Using this class, you can create, retrieve, update, and delete records in a database with simple Python code without having to write one or two SQL statements.
- The views.py file contains the business logic of the page. The Latest_books () function is called a view.
- urls.py points out what kind of URL to call a view. In this example/latest/url will call the Latest_books () function. In other words, if your domain name is example.com, anyone browsing the URL http://example.com/latest/will call the Latest_books () function.
- Latest_books.html is an HTML template that describes how the page is designed. Use a template language with basic logic declarations, such as {% for book in book_list%}
Summary: In combination, these partially loosely followed patterns are called model-view-controller (MVC). Simply put, MVC is a method of software development that separates the definition of code and the method of data access (the model) from the request logic (Controller) and the user interface (view).
The key advantage of this design pattern is that the various components are loosely coupled. Thus, each Django-driven Web application has a clear purpose and can be changed independently without affecting other parts. For example, a developer changes a URL in an application without affecting the underlying implementation of the program. Designers can change the style of the HTML page without touching the Python code. The database administrator can rename the data table and simply change one place without needing to find and replace it from a large stack of files.
MVC is really simple (Django framework)