Iv. defining the data Model
The V in MVC is the application of views.py and Templates,c is the urls.py file under the project.
The M in the MVC model is the modles.py file under the application blog, specifically defining the database
1. Application directory blog, modles.py file definition data table
Description: The data type is defined in the official website, as described in the IP address type: https://docs.djangoproject.com/en/dev/
Class Host (models. Model): hostname = models. Charfield (max_length=50) IP = models. Ipaddressfield ()
2. Check the syntax or logic of the model for errors
Python manage.py Validate
3. Initializing the database
1) In the setting file under the project directory, you can see that the default database is Sqllite3, the database directory is in the Base_dir directory, and the database file is named Db.sqlite3
Take a two-tier path that represents the current script's previous level of directory
2) Check if the system installs the Sqlite3 database, no yum installation
2.1) Enter Dbshell, familiar with Sqlite3
. Help #帮助
. Tables #查看表
. Exit #退出
2.2) After each login, a db.split3 file is automatically generated, or it can be deleted
2.3) or so enter
2.4) View the data table, where the ID is automatically generated
3) Synchronizing the database
Command
Python manage.py syncdb
Description: Automatically generates some of your own tables, as well as certifications
3.1) After initialization, view the table
Description: Syntax and MySQL are basically similar
4) Manage the data and display the database data on the admin page
Modify the admin.py file under Application Blog
Description: Import the table first, then define a class, the parameter is the admin module, inherit the Modeladmin method, list the fields in the table, register the host table and manage the table
Class Hostadmin (admin.modeladmin): List_display = ["hostname", "IP"] Admin.site.register (host,hostadmin)
5) Web view, and add data
6) View the added data on the database
V. Interactive approach to accessing the database
1.web project, into the Python shell, and Ipython almost, more environment variables
Description: Under Other directories, the Web path is not visible
2. Import the host table to view hosts and related methods
3. View host values
4. Add Data
Method One:
n = Host (hostname= "Huangzp2", ip= "192.168.2.232") n.save () nodes = Host.objects.all () nodes.values ()
Method Two:
n = Host () n.hostname = "HUANGZP3" N.ip = "192.168.3.233" N.save () node = Host.objects.all () node.values ()
5.web Page view
6. View and modify elements
VI. View file views.py Access data
1. Modify the urls.py file under the Web project to add a URL access path
views.py file under 2.blog, defining method method (equivalent to command line)
3.web Page view
Vii. data transfer post and get
1. views.py defined post access method in the application directory
def db (req): Print req if req. Post:hostname = req. Post.get ("hostname") IP = req. Post.get ("IP") host = host () Host.hostname = hostname Host.ip = IP host.save () return H Ttpresponse ("OK")
Description: Req. Post or Req.method = = "POST"
2. Note the middleware in the setting.py file under the Web directory, enabling third-party tool method access
3.post Method:
curl-d hostname= "HUANGZP5"-D ip= "192.168.3.235" http://192.168.2.230:8000/db/
Results:
Browser view
4.get Method:
Or else req. Get, because the former may error when accessing ip/db/
DEF DB (req): print req if req. Post: hostname = req. Post.get ("hostname") ip = req. Post.get ("IP") host = host () host.hostname = hostname Host.ip = ip host.save () return httpresponse ("OK") elif req.method == "Get": hostname = req. Get.get ("hostname") ip = req. Get.get ("IP") host = host () host.hostname = hostname host.ip = ip host.save () return HttpResponse ("OK") else: httpresponse (" No data ")
Browser Pass Value:
192.168.2.230:8000/db/?hostname=huangzp6&ip=192.168.3.236
Browser view:
Django topics-Data model, database access, data transfer