1. Download and install the MySQLdb class library
http://www.djangoproject.com/r/python-mysql/
2. Modify settings.py Configuration Data Properties
DATABASES = {' default ': { ' ENGINE ': ' Django.db.backends.mysql ', # Add ' postgresql_psycopg2 ', ' MySQL ', ' Sqlite3 ' or ' Oracle '. ' NAME ': ' Djangodb ', # Or path to database file if using Sqlite3. # The following settings is not used with Sqlite3: ' USER ': ' Root ', ' PASSWORD ': ' Root ', ' HOST ': ' 127.0.0.1 ', c8/># Empty for localhost through domain sockets or ' 127.0.0.1 ' for localhost through TCP. ' PORT ': ' 3306 ', # Set to empty string for default. }}
After modifying into DOS into the project directory execute Python manage.py shell command Start interface Enter the code to verify that the database configuration is successful. No error is successful!
>>> from django.db import connection>>> cursor = Connection.cursor ()
3. Create a Django App
A project contains one or more of these apps. The app can be understood as a collection of features. For example, the Product Management module contains additions and deletions of the function, you can call product management an app. Each Django app has its own models,views, which is easy to migrate and reuse.
DOS into the project directory execute Python manage.py startapp products generate catalog file as follows:
products/ __init__.py models.py tests.py views.py
4, write Models
From django.db import models# Create your models Here.class company (models. Model): full_name = models. Charfield (max_length=30) address = models. Charfield (MAX_LENGTH=50) tel = models. Charfield (Max_length=15,blank=true) class Product (models. Model): product_name = models. Charfield (max_length=30) Price = models. Floatfield () stock = models. Integerfield (max_length=5) Company = models. ForeignKey (company)
5. Model installation (Modify settings.py)
Installed_apps = ( ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.contrib.sessions ', ' django.contrib.sites ', ' django.contrib.messages ', ' django.contrib.staticfiles ', # Uncomment The next line to enable the admin: ' django.contrib.admin ', # uncomment the next line to enable admin documentation : ' Django.contrib.admindocs ', ' djangomysqlsite.products ',)
Use Python manage.py validate to check that the model's syntax and logic are correct.
Without errors, execute the Python manage.py syncdb to create the data table.
Now you can see that your database has created several other tables in addition to Products_company,products_product, which are the tables that the Django management background requires.
6, simple additions and deletions to check
Enter the Python manage.py shell
from DjangoMysqlSite.products.models import company>>> c = Company ( Full_name= ' group ', address= ' West Lake ', tel=8889989) >>> c.save () >>> company_list = Company.objects.all () >>> company_list>>> C = Company.objects.get (full_name= "group") >>> C.tel = 123456>>> C.save () >>> c = Company.objects.get (full_name= "group") >>> C.delete () #删除所有 >>> Company.objects.all (). Delete ()