Deletions orm django static configuration file data fields change search database using MySQL

:( template syntax only about two cases)
 1 : {{obj}} variables associated
 2 : {% for i in List% } logically related 
   { % endfor% } 

Today's content: 

create a route: the route must go to the newly created register settings 
    created: python.manage.py startapp app02 
    registration: 1 the .app (support abbreviated)
          2 .app02.apps.App01Config (full name) 
to start django project, make sure that only one port in a django project occupied, otherwise easily lead to bug 
users to access to resources in the url, only the url opened resources, you can have access to ( ***** ) 

HTML file in the default templates folder on all 
static files: front-end framework, pictures, css, js .. the default in static folder under 
static folder created by default subfolder 
    css folder: the current web site style files 
    js folder: the current web site js file 
    img folder: current site All image files
Static configuration file: 
    1 .STATICFILES_FIRS = [ 
            the os.path.join (base_dir, ' static ' )
     2 .STATICFILES_FIRS = ( 
             the os.path.join (base_dir, ' static ' ), 
             ) 
    Note: 1 . Resources are required end manually exposed to the user, so long as you enter the static folder path to a particular document, to be able to visit
        2 .STATIC_URL = ' / static ' in static only interfaces prefix, instead of the name of the folder
        3 . dynamic resolution between static text, dynamic access ports shallow drunk 
            { % Load static % }
             <Link the rel = "this stylesheet " the href = " {% static 'on Bootstrap-3.3.7-dist / CSS / bootstrap.min.css'}% " > 
            <Script the src = " {% static' on Bootstrap-3.3.7-dist / JS / on Bootstrap. min.js'%} " > </ script> 

Key: ***** 
Python manage.py makemigrations: default generate migration scripts (data migration records) to all APP 
Python manage.py the migrate: 
    the migrate done: 
        1 . the translation of the relevant migration script into SQL statements, execute the SQL statement in the database.
         2 . If the SQL statement is executed without problems, it will be the migration script name in the record to django_migrations. 
    how to determine which migrate migration script needs to be performed : 
        1 + . The code migration scripts and database migration script django_migrations in comparison, if found in the database, without this migration script, it will execute the migration script.
These two command calls the default is global, that operate on all model or migrate files recent changes. If you want to operate on the part app, it is necessary thereafter adding name app: 


form is the default form ge (url data) mode request, may be modified by the method of post 
    injection: a swap to the intermediate post, the settings need Note built csrf, 
        data submitted by post time, django will check whether there is a csrf random string, if no error will be. 
        # ' django.middleware.csrf.CsrfViewMiddleware ' , 
    EG: HTTP: // 127.0.0.1: 8000 / login / username = lina & password = 123? 

handling view function: 
mode 1: 
IF request.method == ' the GET ' : 
    Print (request.method) # manner and capable of acquiring distal request string is all uppercase 
    return the render (Request, ' the login.html ' ) 
elif request.method== ' the POST ' :
     ) returnThe HttpResponse ( " get brother " ) 
Second way: 
IF request.method == ' the POST ' :
     return the HttpResponse ( ' coming baby ' )
 return the render (Request, ' the login.html ' ) 
      Note: The view function will normally processed get request 

to retrieve a front data: 
    request.method acquisition request mode 
    the GET: 
        `` request.GET`` acquiring data (similar to a large dictionary) filed distal get 
        the value: value is a list 
            mode a:. `` request.GET`` get ( ' XXX ' 
                    `` request.GET`` .getlist ( ' XXX ' ) 
            Second way: `` request.GET`` [ ' XXX ' ] 
        Note: . 1 .get default data fetch a list of the last
             two data .getlist will take out a full list
             3 strongly. recommend use the second approach, the list will be error-free data 
    POST 
        request.POST data acquisition front-end post submission (it is like a big dictionary) 
        value 
            request.POST. gET ( ' username ' ) # Although the value is a list but the default list just take the last element 
            # strongly recommended that you use the form values in brackets 
            # if you want directly to list all out ( ****** ) 
            request.POST.getlist ( 'Hobby ' ) 
 
configuration database:
as the default django sqlite database, then use other databases to do the following two steps; 
1 Modify Settings. 
    DATABASES = {
         ' default ' : {
             ' ENGINE ' : ' django.db.backends.mysql ' ,
             ' NAME ' : ' FEI ' ,
             ' the HOST ' : ' 127.0.0.1 ' ,
             ' PORT ' : 3306 ,
             'USER': 'root' ,
             ' PASSWORD ' : ' 678 666 ' ,
             ' the CHARSET ' : ' UTF8 ' 

        } 
    } 
2 . In the project name / application file name __init__ pymysql allowed to connect to the database using, instead MySQLdb 
    Import pymysql 
        pymysql.install_as_MySQLdb () 
Note: settings of k, v k for a pair of key caps 


django orm: object-relational mapping 
class ----------> table 
Object --------> a record 
Object properties ---> field value corresponding to the 
model class: 
 class M (models.Model): 
    IDModels.AutoField = (primary_key = True) 
    username = models.CharField (max_length = 255 ) 
    password = models.IntegerField () 
Note: 1 . In the django orm can not write the primary key field, django will help you create
    2 yourself. created, django will not help you create
    3 .charfield you must specify the parameters max_length
    4 data after .models are subject to change, you need to perform python manage.py makemigrations with python manage.py migrate, both are indispensable 

add table fields deletion check: 
    by: 
        a . default value field is set to a new 
        addr = models.CharField (= MAX_LENGTH 32 , default = ' China ' )
         2 to a new field is set to null.
        Age = models.IntegerField ( null = True) # This field allows null 

     deletion: This field models commented out and execute the update command 
        NOTE: 1 . Remove all data fields corresponding to all performed after
            2 is not used. delete (******* in the true sense ) with caution 


additions and deletions to the data change search: 
     search: 
        First models module from app01 Import models 
        search of ways: 
            1 .models.User.objects.all ()
             2 . . models.User.objects GET (= XXX XXX)
             . 3 .models.User.objects.filter (= XXX XXX)
             . 4 .models.User.objects.filter (= XXX XXX) .fist () 
          Note: 1.get method to get the data object itself, the query condition does not exist when the error is not directly recommended by
               2 .filter method to get a list, a list of the data within the object itself, when the query does not exist will not be an error, it returns an empty list ; 
                filter support multiple parameters, and the relationship between the parameters 
              . 3 .all method to check all data objects can be directly obtained queryset a query point of view inside the sql statement corresponding 
                queryset as a list of objects to be operated, the index support (only positive index number does not support negative), support sections (cut out of the new queryset objects) 
              4 .filter () FIST get a list of data objects with this recommendation. 
              Key: 
                    with an index value when the list is not will complain when data; 
                    a first value though is in accordance with the internal index value but no data no error will be returned None of its internal mechanism has been optimized 


     by: 
        method 1.models.User.objects.create (username = username , password = password)
             2 .user_obj = models.User (username = username, password = password)
              user_obj.save () 
     deleted :( get from the front to delete the data id, according to id delete) 
        models.User.objects.filter (condition) .delete () 

     change :( identify data to be modified, allowing users to render to page modified, then passes the rear end modified) 
       . 1 . models.User.objects.filter (condition) .Update ()
        2 .edit_obj.username = username 
         edit_obj.password = password 
         edit_obj.save () 

       Note: . 1 .filter take to be a list, he belongs to the batch operation will modify all at once for a similar cycle
            2 . will start to finish all the fields of all changes again, very low efficiency.

 

Guess you like

Origin www.cnblogs.com/wyf20190411-/p/11531087.html