Summarize the lesson django web framework

# ## the OSI seven layer model 
- Application Layer
 - that layer application layer
 - Session Layer
 - Transport Layer                
 - Network Layer
 - Data Link Layer   
 - Physical layer

 - Socket Layer virtual socket is located between the application layer and transport layer, a group of Interface

 - C / SB / S

 - Baidu server socket server
     --1 . binding ip and port
     - 2 . wait connected
     --5 . accepts data
     - 6 . return data
     - 9 . disconnect    

 - browser client socket
     --3 The connection server
     --4 transmission data.
     --7 accept data.
     - 8 . browser rendering data
    - 9 . Disconnect     
    

# ## the HTTP 
-. 1 requests and responses.
     - Request (request) message sent from the server to the browser of 
     - Format:
         - ' request URL path protocol version Method 
        - K1: V1
         - K2: V2
            
         - request data '
         - the GET did not request data

     - the response (response) server to the browser back message
     - format:
         - ' the HTTP / 1.1 status code status description 
        - K1: V1
         - K2: V2
        
         - response data (HTML text) '
 
- 2 request Total of 8
     - gET get a page
     - POST to submit data
    
--3 . Status Code
     - 1xx 
     - 2xx 200 is 201 
    - 3xx Redirection
     - Error 4xx Request 403 404 
    - Error 5xx Server 
        

# ## Web function frame 
-. 1 .socket messaging
 - 2 . Return different results based on a different path
 --3 . return to the dynamic page (the replacement string) template rendering
    
 - Django the Flask Tornado 

# ## 1. Download and install 
--1 command line:.
     - PIP install Django == 1.11.22 -i HTTPS: //pypi.tuna .tsinghua.edu.cn / the Simple / 
    - PIP install Django does not specify a version number, default install the latest version

 - 2 .pycharm          
    
        
# ## 2. create a project 
--1 . command line
     - switching a store directory project
    - django- ADMIN startproject Project name
    
 - 2 .pycharm
     - File _> new new Project _> on the left choose Django - "Enter the path of the project -" Select interpreter - "Creating a 
    
        
# . ## 3 Start 
--1 Command OK
     - switch to the root directory of the project manage.py
     - Python manage.py the runserver   # default 127.0.0.1:8000 
    - Python manage.py the runserver 80 # specify the port number 127.0.0.1:80 
    - Python manage.py the runserver 0.0.0.0 : 80 # to share and specify the port number 0.0.0.0:80
     
- 2 . PyCharm 
        
    
# . ## 4 configuration 
    - the root of BASE_DIR project
     - configure DIRS TEMPALTES template = []
     - dATABASES database
    = STATIC_URL - ' / static / '   Alias static files
    
     - STATICFILES_DIRS = [     # after the static configuration file is not found first of all look at this 
        os.path.join (base_dir, ' static ' ) 
    ] 

    
# ## 5.APP 
    - create
         - command line: python manage.py startapp app name
        
     - registration: 
        INSTALLED_APPS = [
                 ' django.contrib.admin ' , 
                ... 
                ' app01 ' ,
                 ' app01.apps.App01Config '   # recommended way
            ] 

    
# ## 6 POST request submitted. 
    Present case Note MIDDLEWARE 
      ' django.middleware.csrf.CsrfViewMiddleware ' 
     
    
# ## 7.urls.py 
     - correspondence relationship between path and function url 
         the urlpatterns = [ 
                url (R & lt ' ^ ADMIN / ' , admin.site.urls), 
                URL (R & lt ' ^ Login / ' , views.login), 
            ] 


# ##. 8 write function.   
    - the views.py under the App
    
     - DEF XXX (request):
         - request - request related content
         -request.method - the GET request method POST 
         - data sheet form POST request submitted} {- of request.POST   
        
         - return Response
        
     - Response:
         - from django.shortcuts Import the render, the HttpResponse, the redirect
         - the HttpResponse - "Returns a string  
         - the render (Request, ' template filename ' , {}) - "return is a page
         - redirect ( ' redirection address ' ) -" redirect
     
     - template templates      

     
# ## 9.orm object-relational mapping ( Relational Mapping Object) 
- Django using the configuration mysql database:
     --1 . create a mysql database
    - 2 . In configuration database settings:
     --3 . Use pymysql mysql database module is connected 
        with the settings in __init__.py in the same directory: 
        Import pymysql 
        pymysql.install_as_MySQLdb ()    
     -. 4 create a table, models in the app. .py write categories: ``
    `Python 
    from django.db Import Models 


    # the Create your Models here Wallpaper. 
    class the UserInfo (models.Model): 
        username = models.CharField (MAX_LENGTH = 32)   # VARCHAR (32) 
        password = models.CharField (MAX_LENGTH = 32 ) 

     `` `
    
     -. 5 . database migration command
         - Python manage.py makemigrations   #Create a migration file records the changes recorded on models.py 
        - the migrate Python manage.py           # perform database migration   
        
    
# ## 10 ORM specific operations:. 
`` `Python 
    RET = models.Publisher.objects.all ()   # to query all data Object list 
    for I in RET:
         Print (I, type (I))    #   corresponding to the object database a data 
        Print (i.name, i.id) # attribute value of the field 

    models.Publisher.objects.filter (name = publisher_name) search is not empty list 

    models.Publisher.objects.create (name = publisher_name) new 

    models.Publisher.objects.filter (the above mentioned id = the above mentioned id) .Delete () delete


    Modify 
    publisher_name = request.POST.get ( ' publisher_name ' )
     # edit 
    pub_obj.name = publisher_name   # only change in memory 
    pub_obj.save ()   # save the changes to the database 
`` ` 
    
    
# ## 11. Template 
` `` html 
{ % for Pub in RET% }
       <TR> 
        <TD> pub.id {{}} </ TD> 
        <TD> pub.name {{}} </ TD> 
      </ TR> 
{ % endfor% } 
`` `



     
    

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/11258412.html