Forty-sixth chapter: session validation

1. djagno ORM column data types
 
  MySQL djagno
  int interfiled
  
  
  
  
  parameters:
   null: to true
   default: setting defaults
   max_length: Maximum length
   unique: unique index
   United unique index:
    class Meta -:
     unique_together = (
      ( "ID", 'name')
     )
   Normal joint index:
    class Meta:
     index_together = (
      ( "the above mentioned id", 'name')
     )
   
   db_index: set the general index
   
   db_column: set column name
   
 2. Djagno admin column types:
  
  EmailFiled: verify the mailbox is correct
  
  fileFiled: verify that the file upload
  . . . . .
  
  Parameters: 
   blank: True, false
   versobse_name: display field name
   whether editable Admin can edit the
   message in this field help_text Admin
   display content selection choices Admin frame, does not change the data in memory to avoid cross-table operation
   choices = (
    (. 1 'M'),
    (2, 'F')
   )
   Gender = models.IntegerField (choices = chocies)
  
  
 3. tab:
  
  
  built tab:
   
   the paginator
  
   disadvantages:
    page can display all
  
  custom tab:
   
   1. models.xxxx.objects.all () [Start: STOP]
   2. judgment of extreme
    leftmost:
     Half
     
    rightmost:
     Half
   
  PS:
   own tab tool used:
    1. class class
    2. DataTables (https://datatables.net)
   
    
 4. CSRF:
  
  
  Settings:
   MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
   ]
  
  @csrf_exempt  @csrf_protect
  
 

Today contents:
 
 1. Templates (always the effect of rendering pages)
  
  - the basic data types to render
   a variable.
   B list.
   C. Dictionary
   d. Dictionary list sets
   
  - Master
   layout.html page layout
    {mycss% Block%}
    
    {%}% endblock
   child inherits:
    {the extends% "layout.html"%}
    
    {%}% mycss Block
     Link
     Script the src
    {% endblock% }
  
  - import
   
   wrote a very beautiful html code (html page)
   
   {% the include "html page"%}
  - Built-in functions
   
   Python:
    STR = "Zekai"
    str.upper ()
    str.lower ()
   
   Master Language:
    
    
  - Custom Function
   configuration:
    A, creating the app module templatetags
    b. 创建xx.py
   
   
   - simple_filter:
    
    from django import template
    register = template.Library()
    register.filter @ ()
    DEF my_func (Val, arg1):
     return Val + arg1
    
    Note:
     1. Only pass a parameter
     with no spaces between the function name and parameter 2.
    Usage:
     {% the Load xx%}
     {{name | my_func: 'kkkk'}}
   
   - the simple_tag:
   
    from Django Import Template
    register = template.Library()
    
    @register.simple_tag()
    def my_func(val, arg1):
     return val + arg1
    
    用法:
     {% load xx %}
     {% my_tag 'zekai' 'is' 'jjj' %}
   
 2. cookie and session (********************************)
  
  Principle:
   - the cookie:
    exists in the client browser a random string of key-val (value pairs)
    
    returned from the server
     { "key": cookie value}
   - session
    
    key containing user sensitive information exists in the server for
    
    {
     "Cookie value": { "name": 'Zekai', 'Age': 18 is},
     "Cookie value 1": { "name": 'zekai2' , 'Age': 34 is},
     "Cookie value 2": { "name": 'zekai3', 'Age': 45},
     "Cookie value 3": { "name": 'zekai4', 'age': 56 }
    }
   
   
   
  specific use:
   
   ### acquires session
   ### query session value
   # Print (request.session.keys ()) ### dict_keys ([ 'name', 'pwd', 'Age'])
   # Print ( request.session.values ()) ### dict_values ([ 'Zekai', '123', 12 is])
   # Print (request.session.items ()) ### dict_items ([( 'name', 'Zekai' ), ( 'pwd', '123'), ( 'Age', 12 is)])
   
   
   
   ### set values session
            request. session['name'] = username
            makes request.session [ 'pwd'] = pwd
            makes request.session [ 'Age'] = 12 is
   
   
   # user session random string
   request.session.session_key
 
   # Session expiration date data of all less than the current date deletion
   request.session.clear_expired ( )
 
   # check the user's session whether random string in the database
   request.session.exists ( "session_key")
 
   # delete all current Session data users
   request.session.delete ( "session_key")
 
   request.session.set_expiry (value)
    * If the value is an integer, session will expire after some number of seconds.
    * If the value is datatime or timedelta, session will expire after this time.
    * If the value is 0, the user closes the browser session will fail.
    * If the value is None, session will depend on the global session expiration policy.
   
  
   
  The storage medium may be used:
   
   1. SESSION_ENGINE = 'django.contrib.sessions.backends.db' # engine (default)
    
    Key when SESSION_COOKIE_NAME = "sessionid" # Session cookie is stored on the browser, namely: sessionid = random string (default)
    SESSION_COOKIE_PATH = "/" # save the cookie path of Session (the default)
    SESSION_COOKIE_DOMAIN = saved None # Session of cookie domain (default)
    SESSION_COOKIE_SECURE = False # whether Https transfer cookie (default)
    SESSION_COOKIE_HTTPONLY = True # whether the Session cookie only supports http transport ( default)
    SESSION_COOKIE_AGE = 1209600 # Session of cookie expiration date (two weeks) (default)
    SESSION_EXPIRE_AT_BROWSER_CLOSE is # = False whether to close the browser makes Session expired (default)
    SESSION_SAVE_EVERY_REQUEST = False # is
   
 
   2. SESSION_ENGINE = 'django.contrib.sessions.backends.cache' # engine
     SESSION_CACHE_ALIAS = 'default' # alias buffer used (the default cache memory, may be memcache), provided by the alias cache dependency
    
    
     when SESSION_COOKIE_NAME = "sessionid" # Session's cookie on the browser key, namely: sessionid = random string
     SESSION_COOKIE_PATH = "/" # saved Session cookie path
     SESSION_COOKIE_DOMAIN = None # Session saved the cookie domain name
     SESSION_COOKIE_SECURE = False # are Https transfer cookie
     SESSION_COOKIE_HTTPONLY = True # whether the Session cookie only supports http transmission
     SESSION_COOKIE_AGE = 1209600 # Session of cookie expiration date (two weeks)
     SESSION_EXPIRE_AT_BROWSER_CLOSE is # = False whether to close the browser makes Session expired
     SESSION_SAVE_EVERY_REQUEST = False #
   
   3. SESSION_ENGINE = 'django.contrib.sessions.backends.file' engine #
    SESSION_FILE_PATH = None # cache / var / folders / d3 / j9tj0gz93dg06bmwxmhh6_xm0000gn / T: file path, if it is None, then use the tempfile module obtains a temporary address tempfile.gettempdir () # as
   
   
    key when SESSION_COOKIE_NAME = "sessionid" # Session stored in a cookie on your browser , namely: sessionid = random string
    SESSION_COOKIE_PATH = "/" saved # Session cookie path
    SESSION_COOKIE_DOMAIN = saved None # Session of cookie domain
    SESSION_COOKIE_SECURE = False # whether Https transfer cookie
    SESSION_COOKIE_HTTPONLY = True # whether the Session cookie only supports http transmission
    SESSION_COOKIE_AGE = 1209600 # Session of cookie expiration date (two weeks)
    SESSION_EXPIRE_AT_BROWSER_CLOSE is = False # if you close the browser is such that session expired
    SESSION_SAVE_EVERY_REQUEST = False      
   
   4. Configuring the settings.py
 
    SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' #
   
   
   
   ## distributed storage session
   
  added:
   relational database based :( disk)
     SQLLite MySQL Oracle DB2
   
   Philippine relations :( memory-based database)
    Redis, mongdb, memcahce
      
 
 3. middleware (Django request lifecycle)
  
  - class class
   
   - process_request: a method request must pass through
   - process_response: a method must pass through the response
   
   - process_view
   
  - Settings: 
   MIDDLEWARE = [
    'django.middleware .security.SecurityMiddleware ',
    ' django.contrib.sessions.middleware.SessionMiddleware ',
    ' django.middleware.common.CommonMiddleware ',
    ' django.middleware.csrf.CsrfViewMiddleware ',
    ' django.contrib.auth.middleware.AuthenticationMiddleware ',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'm1.M1',
    'm2.M2'
   ]
   
  - Application:
   preprocessing, when the code is written in the middleware
   determines whether the value of the IP is blacklisted
   .....
  

 4. MTV and MVC
  
  
  MVC:
   
   design mode project directory structure of the
   
   
   customer ordering -------- ------> waiter ------ -------- needs treatment menu> chef
        <------------------ <-------------------------  
   
   (browser) -------------------> functions or handling ------------------> database
              business logic
   views: controllers models :
            loginController.py LoginModel.py
            UserController.py UserModel.py
    large html page                        
    
   
  the MVC
   
   Django:
    M: Models
    T: the Templates (various html page) corresponding to views
    V: the views (view handler) corresponds controllers
   
  
  

Guess you like

Origin www.cnblogs.com/haojunliancheng/p/11209778.html