1. The famous two modes MVC and MTV models
The famous MVC pattern in the field of Web server development
Web application layer: model (M) model is responsible for the mapping between business objects and database (ORM)
View (V) View is responsible for the interaction with the user (page)
Controller (C) The controller accepts the user's input and invokes the model and view to complete the user's request
They are connected in a plug-in, loosely coupled way.
Django's MTV pattern is essentially the same as MVC
Django's MTV is the value:
The M Model is responsible for the relational mapping (ORM) between business objects and the database.
The template (T Template) is responsible for how the page is displayed to the user (html).
The View (V View) is responsible for the business logic and calls Model and Template when appropriate.
A URL dispatcher is also required. Its function is to distribute page requests of URLs to different Views for processing, and Views then call the corresponding Model and Template.
2. Django download and basic commands
1) Download Django
pip3 install django #Basic default installation
pip install django==1.11.7 #Select version to install
pip install django==1.11.7 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com #Domestic mirror installation
2) Create a django project (project)
django-admin.py startproject (project name)
manage.py This is a tool in the Django project, it can call the django shell and database, etc.
settings.py This is the Django project settings file, which contains the project default settings, including database information, debug flags, and some other variables that work
urls.py This is the controller responsible for mapping URL patterns to the application.
3) Create an application in the (project name) directory
python manage.py startapp blog #blog is the App name
4) Start the django project
python manage.py runserver 8090
Our django is up and running! Can be accessed at http://127.0.0.1:8090
Simple example:
url controller:
from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path('index/',views.index), #Add a new record ]
view:
from django.shortcuts import render # Create your views here. def index(request): #index walk this function import datetime now=datetime.datetime.now() ctime=now.strftime("%Y-%m-%d %X") return render(request,"index.html",{"ctime":ctime}) # Template Syntax Variables
stencil:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h4>current time:{{ ctime }}</h4> #Template Syntax Variables </body> </html>
Access: You can visit http://127.0.0.1:8090/index.html