随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

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()
复制代码

 

posted on   momingliu11  阅读(2187)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-05-06 命令返回值
2013-05-06 csvde导出计算机/用户信息
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示