Introduction to the

Introduction of the MVC

  • Model-view-controller, the full name of MVC, was first proposed by TrygveReenskaug in 1978. It is a software design mode invented by Xerox PARC in 1980s for Smalltalk, a programming language. It is designed to apply the traditional input, processing and output tasks into the graphical user interaction model. With the advent of standard input input devices, developers only need to focus on the analysis and implementation of business logic. It has since been recommended as a design pattern for Oracle’s Sun Java EE platform and is increasingly popular with developers using ColdFusion and PHP. Although the original way of division of labor is no longer used, the idea of division of labor has been used and widely used in software engineering, which is a typical and widely used software architecture mode. The MVC architecture provides a way to partition objects (which are used to maintain and represent data) by function in order to minimize the coupling between objects. In the current MVC structure, the Model represents the application’s data and the business rules that control access to and modification of that data. Typically, models are used as software approximations of a real-world process, and when defining a model, generic simple modeling techniques can be used. It notifies the View when the model changes and provides the View with the ability to query the state of the model. It also provides controllers with the ability to access application functionality encapsulated within the model. A View is used to organize the contents of the model. It takes the data from the model and specifies how the data should behave. Vision is responsible for maintaining consistency in data representation as the model changes. The Controller will be informed of the user’s request at the same time. A Controller defines the behavior of an application; It is responsible for interpreting user requirements from the vision and mapping these requirements into corresponding behaviors that are implemented by the model. On a standalone GUI client, the user request might be some mouse click or menu selection. In a Web application, they might take the form of SOME HTTP request from a client to GET or POST. The behavior implemented by the model includes processing the business and modifying the state of the model. Based on user requirements and the results of model behavior, the controller selects one to be viewed as a response to user requests. Usually a set of related functions corresponds to one controller.
  • The core idea of MVC framework is: decoupling, reducing the coupling between different code blocks, enhancing the extensibility and portability of code, and realizing backward compatibility
  • The current mainstream development languages such as Java, PHP, Python all have MVC framework
  • M is Model, mainly encapsulates the access to the database layer, embedded ORM framework, object-oriented programming to operate the database, without considering the difference of the database, simple configuration can complete the database switch
  • V full spell for View, used to encapsulate the results, embedded template engine, dynamic display of data
  • C, spelled Controller, is used to receive GET or POST requests, process business logic, interact with Model and View, and return results

Django profile

  • Django, pronounced ‘djangos’, is an open source Web development framework written in Python that follows an MVC design. The framework was developed by Lawrence Publishing Group to develop news-focused websites and released under the BSD license in July 2005. The name comes from the Belgian jazz musician DjangoReinhardt, who was a gypsy who played mainly the guitar and also the violin. Due to the rapid development of Django in recent years, more and more widely used, IT was selected by SDTimes, a well-known IT development magazine, as the 2013SDTimes100, ranked 6th in the category of “API, libraries and frameworks”, is considered to be the leader in the field. The main purpose of Django is to make it easy and fast to develop database-driven websites. There is a strong emphasis on code reuse. Multiple components can easily serve as “plug-ins” to the framework. Django has many powerful third-party plugins, and you can even easily develop your own toolkit. This makes Django very extensible. It also emphasizes rapid development and DRY(Don’t Get yourself) principles.

  • The Django framework follows an MVC design and has a name: MVT

  • V is a View, which has the same function as C in MVC. It receives HttpRequest, processes business, and returns HttpResponse

  • M is Model, which has the same function as M in MVC. It is responsible for data processing and embedded with ORM framework

  • T is fully composed as Template, which has the same function as V in MVC. It is responsible for encapsulating and constructing the HTML to be returned, and the Template engine is embedded

A virtual environment

  • During development, Python packages can be networked when needed
Sudo PIP install Package nameCopy the code
  • Using the above command, package will be installed in/usr/bin/local/python3.9 / disk – under the package
  • If you want to develop multiple projects on the same machine, you need to use different versions of the same package. If you use the command above, install or update in the same directory, the other projects must not run.
  • Solution: Virtual environments
  • Virtual environments can be set up as independent Python environments, so that the running environment of a single project is not affected by other projects
  • Virtual environments can be set up as independent Python environments, so that the running environment of a single project is not affected by other projects
  • To create a virtual environment, run the following commands
Mkvirtualenv Virtual environment name for example, mkvirtualenv py_djangCopy the code

use

  • The commands for using the virtual environment are as follows
  • After writing out the first part of the name, you can complete it with the TAD key
Workon Virtual environment name Example: workon py_DjangoCopy the code

exit

  • To exit the virtual environment, run the following command:
deactivate
Copy the code

delete

  • Command to delete a virtual environment
Rmvirtualenv Virtual environment name Example: Deactivate and then delete rmvirtualenv py_djangoCopy the code

Package operation

  • Python packages can be manipulated in a virtual environment using the PIP command
  • Install command
PIP install Package nameCopy the code
  • Check the command
pip freeze
Copy the code

Django installation package

  • Next, learn how to use Django. Take version 1.8.2, which is stable, widely used, and well-documented
  • Install the django1.8.2 package
PIP install django = = 1.8.2Copy the code