1. Create a Django project called Djangodb_demo
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7B/64/wKioL1bNB8LimHpNAAGBIitSDS4002.png "title=" QQ picture 20160224092844.png "alt=" Wkiol1bnb8limhpnaagbiitsds4002.png "/>
2. Using the MySQL Database
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7B/64/wKioL1bNCBfy3xOWAAD359LxGMM124.png "title=" QQ picture 20160224093009.png "alt=" Wkiol1bncbfy3xowaad359lxgmm124.png "/>
2. Create a web-named app under the Djangodb_demo project
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7B/65/wKiom1bNB9rif6q0AACmBweM6AQ868.png "title=" QQ picture 20160224093058.png "alt=" Wkiom1bnb9rif6q0aacmbwem6aq868.png "/>
3. Create a new models.py file under the Web
From django.db import Modelsclass UserInfo (models. Model): Username = models. Charfield (maxlength=50) password = models. Charfield (MAXLENGTH=50)
At the same time, you need to add the Web to your project in settings.py
The directory structure is
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7B/64/wKioL1bNCMXQIJ9UAAA6jo1bCPU645.png "title=" QQ picture 20160224093304.png "alt=" Wkiol1bncmxqij9uaaa6jo1bcpu645.png "/>
4. Configure the connection database information in the settings.py file of the Djangodb_demo project
DATABASES = {' default ': {' ENGINE ': ' Django.db.backends.mysql ', ' NAME ': Djangodb_demo, ' USER ': ' R Oot ', ' PASSWORD ': ' 123456 ', ' HOST ': ' 10.10.20.134 ', ' PORT ': ',}}
Engine: The type of database you are using
Name:django project in MySQL Library
User: The username of the connection library
5. Build a library called "Djangodb.demo" in MySQL
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7B/64/wKioL1bNAviDmuvFAACqywdXJbw479.png "title=" QQ picture 20160224090816.png "alt=" Wkiol1bnavidmuvfaacqywdxjbw479.png "/>
Note: Django cannot create a library, but can create a table
6. Create a table using Django
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7B/64/wKioL1bNA42wirfzAACvgoWI-1s908.png "title=" QQ picture 20160224091046.png "alt=" Wkiol1bna42wirfzaacvgowi-1s908.png "/> Right-click Project name to create a table using Django migrate
Operations to perform: synchronize unmigrated apps: staticfiles, web, messages Apply all migrations: admin, contenttypes, auth, sessionssynchronizing apps without migrations: creating tables... creating table web_userinfo running deferred sql ... installing custom sql ... running migrations: rendering model states... done applying Contenttypes.0001_initial... ok applying auth.0001_initial... ok applying admin.0001_initial... ok applying contenttypes.0002_remove_content_type_name ... ok applying auth.0002_alter_permission_name_max_length... ok applying auth.0003_alter_user_email_max_length... ok applying auth.0004_alter_user_username_opts... ok applying auth.0005_alter_user_last_login_null... ok applying auth.0006_require_contenttypes_0002... ok applying sessions.0001_ initial... okfinished "/root/eclipse_data/djangodb_demo/manage.py migrate" execution.
Successful execution!
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7B/65/wKiom1bNCjaT7QYFAAA7l7bq26k854.png "title=" QQ picture 20160224094030.png "alt=" Wkiom1bncjat7qyfaaa7l7bq26k854.png "/>
You can see that in addition to having a userinfo table in models.py, the others are created by Django itself!
And look at how Django created our userinfo table.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7B/64/wKioL1bNDBzSHcSIAABYa5o2PiQ650.png "title=" QQ picture 20160224094713.png "alt=" Wkiol1bndbzshcsiaabya5o2piq650.png "/> Looks like we've got more tables. primary Key ID field
This article is from the "write-free" blog, please be sure to keep this source http://fangniu.blog.51cto.com/8773628/1744526
Django uses the Django.db module to create a table