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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM