table of Contents
Django framework
What is the Django framework
Django is a high-level Python Web development framework, in order to facilitate people to Web development.
Popular Web framework
Django: large and built-in components and functions very, very much similar to the aircraft carrier
Weaknesses: write some small projects, they might be more cumbersome (overkill)
Flask: small and fine, dapper, carrying components and functions are particularly particularly small, substantially all rely on third-party components,
Weaknesses: limited by the relatively large impact of third-party modules,
If Flask all third-party modules together, can directly turn Django
Tornado: mainly non-blocking asynchronous, when using this framework very quickly, it can be used to develop the game server
the difference
a: socket part b: Match route c: Template grammar
Django:
a: use of someone else's wsgiref (django default)
b: Write your own
c: to write their own
Flask:
a: with someone else's werkzeug
b: Write your own
c: use of someone else's jinja2
Tornado:
a, b, c are to write their own
http protocol
Four characteristics:
1. Based on the TCP / IP application layer protocol function and
2. Based on the request response
3. No state --- cookie, session, token
4. No long connection WebSocket connection ---
Data Format
Request format
Request header
\r\n
Request body
The response status code
A string of numerical language interpreter
Analog Django framework
Django installation
1. Installation cmd
① confirm that Django is already installed
>>> django-admin
② Create a new app
>>> django-admin startproject 项目名
③ Switch to the file directory, open django
>>> python manage.py runserver # runserver后边可以加ip:端口,要空一格
2. Use pycharm installation
① create Django
new project ---->> Django ---->> create
② create app
Tools ---->> run manage.py task
3. The difference between the two installation methods:
1. Use the cud installation will not help you create templates folder
2. Configuration file types will not help you write templates path
4. Configure installation time takes note:
Use cmd:
1. When open Django, you can use python, it can also be used python3.
2. Use the cmd create project templates folder is no need to add your own
Use the time pycharm created:
1. Note the download version of Django, preferably within the range of 1.11.9 - 1.11.13
2. Download the new version of Djanjo prior to Django will top off
3.Djanjo not very compatible version of python3.7, sometimes operations will complain
4. Be sure the same port, unified time can only start a Django project
5.Django automatically restart, and sometimes error do not worry
6. The new app must take effect in Django configuration file types registered, not registered can not be used
5.Django file functions:
Django project name
Project file folder of the same name
settings.py exposed to the profile user-configurable, templates side configuration is here
urls.py view function Routing and a correspondence relationship
manage.py Django entry file
migrations folder database for all records related to operations
admin.py Django admin Admin
apps.py registered app use
models.py put all database-related model
tests.py test file
views.py business logic view function
6.Django white will be three tricks
Returns a string HttpResponse
return to traditional values render html page, you can pass values to the html page
redirect redirect, you can write this site, you can also write the full path
Example:
from django.shortcuts import render,HttpResponse,redirect
HttpRespons
def index(request):
return HttpResponse('一脸懵逼')
render:
def login(request):
user_dic = {'username':'jason','password':'123'}
return render(request,'login.html',{'xxx':user_dic})
redirect:
def home(request):
return redirect('/login') # 重定向,第一种
return redirect('https://www.baidu.com') # 重定向,第二种
Learn new knowledge today
1. Define a function, passing two parameters, env, response
env: all data relevant to the request, the http data processing has become all the advance line dictionary
response: the response data related to all
2.from wsgiref.simple import make_server
from wsgiref.simple import make_server
if __name__ == '__main__':
server = make_server('127.0.0.1',8080,run)
server.serve_forever() # 启动服务端
表示实时监听本机8080端口,一旦有请求过来,会统一交给run函数处理,(调用run函数并传参run(env,response))