创建Django项目(二)——数据库配置

 

2013-08-05 20:53:44

         1.数据库配置
        举例是用MySQL数据库,首先在settings文件中做配置,如下:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',    # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mysite',      # Or path to database file if using sqlite3.
        'USER': 'root',
        'PASSWORD': '111111',
        'HOST': '',     # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',     # Set to empty string for default.
    }
}
但是连接数据库时,报错 ImprotError:no no module named MySQLdb。在网上搜了解决办法,安装MySQL-python-1.2.3.win32-py2.7.exe; 在命令行下执行 python manage.py runserver ,没有报错; 执行python manage.py shell 后,输入下面的代码,结果如下图:
 
 
        2.同步数据库
        在MySQL中创建数据库mysite。
        在命令行下执行python manage.py syncdb命令,syncdb会搜索到INSTALLED_APPS 下所有app,创建数据库表,如下图:
posted @ 2014-01-12 12:12  zifenger  阅读(215)  评论(0)    收藏  举报