Django knowledge organize a (Http protocol, wsgire module, static and dynamic web presentation, python three mainstream framework, Django installation considerations,)

http protocol

Four Characteristics *

  • Based on tcp / ip protocol operated on the application layer
  • Based on the response request
  • Stateless ~~~ cookie session token ...
  • No long link connecting ~~~

Data Format*

  • Request format

    • The first line of the request (request format, protocol version)
    • Request header (a large push k: v key-value pairs)
    • \ R \ n (Space)
    • Request body (get request does not request mode thereof)
  • Response format

    (Request format to the same format)

Request method

  • get request: towards others to data
  • post request: Submit data towards others

Response Status Code *

1xx: server successfully received your data is being processed, you can continue to submit additional data

2xx: server responds successfully (200 ok)

3xx: Redirection

4xx: Request error (404 403 requested resource does not exist to deny access.)

5xx: internal server error

wsgiref module (web services gateway interface)

  1. socket code is a good package for you
  2. http data automatically help you deal with the
    • To seek help when you split the data format http
    • In response there to help you take the time to become consistent with the package http data format

Depending on the functional split into different files py

  • urls.py view function Routing and a correspondence relationship
  • business logic processing views.py rear view function (not just a function of the view function, may be a class)
  • html file templates specialized storage

Rule: if you want to add functionality just need to add a correspondence relationship in urls.py go views.py write a view function

Static and dynamic pages

Static pages: the data is written dead, the same years

Dynamic pages: data is acquired in real time

  • Alternatively the rear end of the acquired time transfer to the front page, using the string to pass data

  • The operation of the dictionary page is transmitted to the distal end, and may be easily

    • By means of a module for rendering module jinjia2

      from jinja2 import Template
      
      temp = Template(data)
      res = temp.render(user={'name':'jason'})
      

Rendering template: use template syntax to achieve the back-end to front-end data transfer html page

The format of template syntax;

  • {} {} Related variables
  • Logically related %% {}

python three major framework

Django

  • Features: large and comes with a particularly large number of functions, similar to the aircraft carrier
  • Cons: sometimes too heavy

Flask:

  • Features: small but excellent, particularly third-party modules, similar to the Rangers (if the flask third-party modules all add up, can exceed Django)
  • Disadvantage: its own particular function less, more dependent on third party modules

Tornado

  • Asynchronous non-blocking
  • Fast, fast hardware to develop a game server
A:socket部分
B:路由与视图函数对应关系
C:模板语法

Django:
    A用的别人的  wsgiref
    B自己写的
    C自己写的
Flask
    A用的别人的  werkzeug(基于wsgiref)
    B自己写的
    C用的别人的  jinja2
Tornado
    三者全是自己写的

Django installation Precautions

  • The computer can not be Chinese
  • A pycharm window represents a project
  • Project name or path where there is not Chinese

Download and install Django

pip3 install django == 1.11.11 (command line)

Backend acquired data to the html page ---> Render Template

jinjia2 AnSo

pip3 install jinja2

Use the command line

创建django项目
    django-admin startproject mysite(项目名)
启动django项目
    python manage.py runserver 
创建应用app
    python manage.py startapp app01(应用名)


验证Django是否安装成功   
django-admin

注意:
1.使用命令行创建django项目 不会自动帮你创建templates文件夹只能自己创建
2.settings文件中 需要你手动在TEMPLATES写配置
    os.path.join(BASE_DIR, 'templates')
    

pycharm use

在启动django项目的时候 你一定要确保一个端口只有一个django项目
一个空的django项目 就类似于一所大学
app就相当于大学里面的各个学院 每个学院都有自己所对应的功能
python manage.py startapp 应用名(尽量跟你的功能呼应)
创建的应用一定要去settings.py文件中注册()
注意:
    新创建的app需要你去settings配置文件中注册
    pycharm只会帮你注册第一个你在创建项目的时候写的应用
'''
项目名
    跟项目名同名的文件夹
        settings.py  暴露给用户的配置文件
        urls.py  路由与视图函数对应关系
    应用名
        migrations文件夹  存放数据库迁移记录的
        admin.py  django后台管理
        apps.py  注册相关
        models.py  模型类 
        tests.py  测试文件
        views.py  存放视图函数
    templates文件夹  存放html文件
    manage.py  django入口文件
'''

Guess you like

Origin www.cnblogs.com/asyouwish/p/11762688.html