Testing under the Django framework (novice self-learning)

  Mainly to record the addition, deletion and modification of MVT mode and MySQL database under the Django framework.

1. First understand the process (thanks to the picture of Lao Wang with shoes)

  

 

 

 

Second, query a single piece of data in the database and display it on the browser

  Not to mention the database configuration in the setting.py file

  1. Project Directory

  

 

 

   2. Browser request

    

 

 

     The test in the address is the test function in the views, to find the corresponding views through the urls

    urls.py code

from django.contrib import admin
from django.urls import path
from dataSystem import views        #+

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index',views.hello),      #+
    path('test',views.test),
]

 

  3. The views.py code

    To introduce the project models.py, get the select class ------- the class is equivalent to the table in the database, the views go to the models to fetch the data, and then get it and pass it to test.html. The test.html file displays the data through check

1  DEF Test (Request):
 2      # query a single object, ID = 1 in all the data, returns a dictionary 
. 3      Check = models.select.objects.filter (ID = 1 ) .get ()
. 4 return the render (Request, ' Test .html ' , { ' check ' : check})

 

  4. The models.py code

  Name and age correspond to the fields in the database. If you want to get the corresponding data, you can get them through models. CharField is equivalent to varchar. The models get the data and return to the views

class select(models.Model):
    name = models.CharField(max_length=20)
    age = models.CharField(max_length=10)

  

  5. template --- test.html code 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
    <h1>{{ check.name }}</h1>
    <h2>{{ check.age }}</h2>

</body>
</html>

  6. Browser display results

  

 

   7. Database data

  

 

Guess you like

Origin www.cnblogs.com/cys52/p/12731912.html