Forty-six, django configuration and CRUD

Routing Access: 
  If you do not add internal slash will automatically add, if coupled with not being given (internal automatically redirects)

Static configuration file:
All static files (css, js, front-end third-party libraries) are placed in static default folder
= STATIC_URL ' / static / '   # static interfaces static prefix with the name of the folder you had nothing to do 
    # static file configuration 
    STATICFILES_DIRS = [ 
    os.path.join (base_dir, " static " ),   # static this is a static folder, local file name 
    os.path.join (base_dir, " static1 " ), 
    os.path.join (base_dir, " static2 " ) 
    ] 
    PS: find words will be followed by a list of all of the static files to find the path to stop immediately, will not be found return error 404 
    general HTTP: //127.0.0.1:8080/xxx/bootstrap-3.3.7-dist/js/ bootstrap.min.js 
    xxx represents the interface, followed by the path, in turn locate a file on a static configuration file folder, to find information

form Form trigger action are two ways to submit data
<input type="submit">
<button></button>

How to submit data to the specified address form and manner of
action submitted by address attribute control
Method: 
        1. Full Path 
             < form Action = "http://127.0.0.1:8080/login/" > 
        2. Write only path suffix 
             < form Action = "/ the Login /" > // will automatically add plus # 127.0.0.1:8080 
        3. Do not write default submission to the current page
The default is to form form get request ( get the data to the server), POST (send request to the server) 
<Action form = "" Method = "POST">

The client request different ways to execute different code logic : 
can the get and post requests, respectively
1. The first embodiment:
DEF the Login (Request):
     # acquisition request submitted by way of client 
    Print (request.method) # request the default way to get a string of uppercase 
    IF request.method == " GET " :
         return the render (Request, " the Login. HTML " )
     elif request.method == " POST " :
       return HttpResponse ( " server receives the data " )

2. The second way :( recommended)
       IF request.method == " POST " :
             return HttpResponse ( " server receives the data " )
          return the render (Request, " login.html " )
         # recommended

POST request (send data to the server):
IF request.method == " the POST " :
         # Print (Request) # <WSGIRequest: the POST '/ Login /'> 
        Print (of request.POST)   # <QueryDict: { 'username': [ 'Kevin'], 'password' : [ '123']}> 
        Print (request.POST.get ( " username " ))   # Kevin, although a list of values, but the time to get the acquired value is a single element 
        # default taken last 
        Print (of request.POST. GetList ( " username " ))   # To disposable take all data values which need GetList
         Print (of request.POST [ "password"])   # 123 is not recommended to use this method 
        return HttpResponse ( " server receives the data " )
      return the render (Request, " login.html " )
    Users get a list of value inside the element occasional need to use getlist scenarios: the user's preference checkbox 
.get will get to the last data value list
 
GET request (data acquisition server):
  Print ( `` request.GET``)   # # <QueryDict: { 'username': [ 'Kevin'], 'password': [ '123']} 
    # acquiring POST request data and get exactly the same way 
    request.POST.get ( " username " ) take a single 
    request.POST.getlist ( " username " ) taking a plurality of

 

Django database connection
1 . Need to modify the configuration file 
        DATABASES = {
     ' default ' : {
         ' ENGINE ' : ' django.db.backends.mysql ' ,
         ' NAME ' : " day41 " ,
         " the HOST " : " 127.0.0.1 " ,
         " PORT " : 3306 ,
         " the USER " : " root " ,
         "PASSWORD": " 123456 " 
        } 
    } 
        PS: The default key must be uppercase

 

2. Tell Django replace it with pymysql default mysql_db modules connect to the database 
1. On your project folder (migrations) below __init__.py
under way 2. You can also use your name (app01) folder __init__. Py

# fixed wording
Import pymysql
pymysql.install_as_MySQLdb () # tells Django replaced mysqldb database connection with pymysql
 
The django ORM:
object-relational mapping:
Class: Table
Object: Table record
object properties: a value of a field recorded
django orm can not help you create the database, but can help you create a table
Tip: a django project uses a library

Database migration command (********) 
python36 manage.py makemigrations (just the record, does not create a table) will change your database records to the little book
python36 manage.py migrate your data changes synchronized recording to the database


CRUD operations database django
1. New data:
 # Way a: 
 USER_OBJ = models.User.objects.create (name = username, password = password)   # return value is an object of the current field 
 # Second way: 
 USER_OBJ = models.User (name = username, password = password) 
 user_obj.save ()   # object calls the save method to save the database 
 Print (USER_OBJ)
  Print (user_obj.pk)   # obtain primary key (primary key without knowing the case can be used) 
 Print (user_obj.id)   # obtain primary key 
 Print (user_obj.name) # get name value 
 Print (user_obj.password) # get password value

2. query data:
   Print (user_list.query) can view the internal implementation of sql statement, as long as Queryset objects can query sql statement
    user_list = models.User.objects . All ()   query the user table all sql data
 
   use a tag href attribute specifies the page jump href can write the whole path path but it is recommended to write suffix 
   <a href= "/reg/" class = " btn btn-success"> Add data </a>
    # path property must be added / / 
    PS: view function must return an HttpResponse object 
    redirection you can write someone else's Web site URL can write server-side return redirect ( " / UserList " )   # internal redirect 


    

 

3. To delete data:
# In the jump tag sends the user id come together <a href= "/delete_user/?delete_id={{ user.id }}" class
     = "btn btn-danger"> Delete </a> 
    Delete: models.User .objects.filter (the above mentioned id = delete_id) .Delete () 
    QuerySet object supports the index value is not recommended that you use the recommended use its own .first () 
    query a way: models.User.objects.filter (the above mentioned id = delete_id) .first ()   retrieves the first, which is a list of objects, if not, it will return an empty object, recommended 
    inquiries second way: USER_OBJ = models.User.objects.get (the above mentioned id = edit_id) # can be obtained directly get, not recommended if there is no will complain 

    

        
         

 

4. Edit:
ID editing target Obtaining
Method 1: using a hidden input tag
<input type = "hidden" name = "edit_id" value = "{{user_obj.pk}}">
way: writing directly form tags in carrying over, a get request
<form action = "/ edit / edit_id = {{user_obj.pk}}?" method = "post">

update:
  first:
models.User.objects.filter (ID = user_id) .update (name = user_name, password = password) recommend the use of high-efficiency

second:
get the current data object
USER_OBJ = models.User.objects.filter (the above mentioned id = user_id) .first ()
user_obj.name = username
USER_OBJ = password .password
user_obj.save ()
     is not recommended, after the object has been modified, all data must change with very low efficiency
 
All data tables associated with the modified model layer inside, as long as you change the need to re-execute the data migration command 
python36 manage.py makemigrations record books to small
python36 manage.py migrate operations to establish a database table field



Guess you like

Origin www.cnblogs.com/wukai66/p/11528443.html