標籤:style blog color os io ar strong 檔案 資料
1.修改settings.py 配置資料屬性
DATABASES = {‘default‘: {‘ENGINE‘: ‘django.db.backends.mysql‘,‘NAME‘: ‘fruitdb‘,‘USER‘: ‘root‘,‘PASSWORD‘:‘hellosandy‘,}}
修改完後進入終端進入項目目錄下執行python manage.py shell命令啟動互動介面輸入一下代碼驗證資料庫配置是否成功。沒報錯則成功!
>>> from django.db import connection>>> cursor = connection.cursor()
2.建立一個Django app
終端進入項目目錄 執行 python manage.py startapp register組建目錄檔案如下:
products/ __init__.py models.py tests.py views.py
3.編寫models
from django.db import models# Create your models here.class Register(models.Model): nickname = models.CharField(max_length=30) email = models.CharField(max_length=50) password = models.CharField(max_length=30) def __unicode__(self): return "%s, %s, %s" % (self.nickname, self.email, self.password)
4.模型安裝(修改settings.py)
INSTALLED_APPS = ( ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘register‘,)
採用 python manage.py validate 檢查模型的文法和邏輯是否正確。
沒有錯誤則執行 python manage.py syncdb建立資料表。
SandymatoMacBook-Pro:fruitproject sandy$ python manage.py validate0 errors foundSandymatoMacBook-Pro:fruitproject sandy$ python manage.py syncdbCreating tables ...Creating table django_admin_logCreating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_groupsCreating table auth_user_user_permissionsCreating table auth_userCreating table django_content_typeCreating table django_sessionCreating table register_register
現在你可以看到你的資料庫除了產生了register_register 外還建立了其它好幾個表,這些是django管理後台所需表暫不管。
Django串連MySQL資料庫