django新建项目,连接mysql数据库
安装django,进入Django目录,运行 python setup.py install
在workplace目录下新建一个名为site01的项目:
cd workplace
django-admin.py startproject site01 ,在workplace目录下自动生成site01目录及其里面的内容
在site01下新建一个名为app01的app:
python manage.py startapp app01
启动项目site01下的WEB服务:
cd site01
python manage.py runserver 0.0.0.0:80
注:
#####################################################
django配置连接mysql数据库
1.python需要先安装mysql模块,否则在django的settings.py中配置mysql连接后,在python manage.py runserver的时候会报错“django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb”
2.配置相应project下的settings.py,默认使用mysql,修改如下:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'testly', #db name 'USER': 'root', #db user 'PASSWORD': '123456789', 'HOST':'192.168.1.1', #db server 'PORT':'3306', #留空表示默认端口 } }
在运行python manage.py migrate同步数据库时,如果出现如下报错则说明连接mysql的用户没有足够的权限,dba添加相应权限即可
注:root'@'192.168.50.74 全部是用户名
安装MySQL-python步骤如下(Windows10):
1.运行python mysqlregistry.py,否则在安装MySQL-python时候会提示找不到python2.7
2.http://www.dlldll.com/ 下载libguide40.dll和 libmmd.dll这两个文件,然后拷贝到C:\WINDOWS/system32/ 目录下
3.http://www.codegood.com/downloads 下载MySQL-python-1.2.3.win-amd64-py2.7.exe
安装完成后在python下可以导入import MySQLdb
mysqlrgistry.py文件内容:
# # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # # written by Joakim Loew for Secret Labs AB / PythonWare # # source: # http://www.pythonware.com/products/works/articles/regpy20.htm # # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath ) def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!" if __name__ == "__main__": RegisterPy()