北京SEO

配置Django使用MySQL数据库的例子

2019/10/10/17:46:40  阅读:1900  来源:谷歌SEO算法  标签: 发外链

Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架了,下面我们就来介绍这款框架配置Django使用MySQL数据库的例子了.

1、安装mysql(Django 安装略):

  1. [root@itchenyi-1Django-1.3.3]#yuminstallmysql-servermysql-devel
  2. [root@itchenyi-1Django-1.3.3]#yuminstallMySQL-python

2、设置Mysql 数据库及用户:

  1. [root@itchenyi-1Django-1.3.3]#servicemysqldstart
  2. [root@itchenyi-1Django-1.3.3]#mysql-uroot-p
  3. Copyright(c)2000,2010,Oracleand/oritsaffiliates.Allrightsreserved.
  4. ThissoftwarecomeswithABSOLUTELYNOWARRANTY.Thisisfreesoftware,
  5. andyouarewelcometomodifyandredistributeitundertheGPLv2license--phpfensi.com
  6. type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
  7. mysql>createdatabaseitchenyi_db;
  8. QueryOK,1rowaffected(0.00sec)
  9. mysql>GRANTALLONitchenyi_db.*TO'itchenyi'@'localhost'IDENTIFIEDBY'yourpassword';
  10. QueryOK,0rowsaffected(0.00sec)
  11. mysql>quit
  12. Bye

3、create a django project:

[root@itchenyi-1 Django-1.3.3]# django-admin.py startproject itchenyi

4、编辑 新建的project 配置文件(settings.py):

  1. [root@itchenyi-1Django-1.3.3]#viitchenyi/settings.py
  2. DATABASES={
  3. 'default':{
  4. 'ENGINE':'django.db.backends.mysql',#Add'postgresql_psycopg2','postgresql','mysql','sqlite3'or'oracle'.
  5. 'NAME':'itchenyi_db',#Orpathtodatabasefileifusingsqlite3.
  6. 'USER':'itchenyi',#Notusedwithsqlite3.
  7. 'PASSWORD':'yourpassword',#Notusedwithsqlite3.
  8. 'host':'',#settoemptystringforlocalhost.Notusedwithsqlite3.
  9. 'PORT':'',#Settoemptystringfordefault.Notusedwithsqlite3.
  10. }
  11. }

5、切换到新建的project 创建数据库和表:

  1. [root@itchenyi-1Django-1.3.3]#cditchenyi/
  2. [root@itchenyi-1itchenyi]#pythonmanage.pysyncdb
  3. Creatingtables...
  4. Creatingtableauth_permission
  5. Creatingtableauth_group_permissions
  6. Creatingtableauth_group
  7. Creatingtableauth_user_user_permissions
  8. Creatingtableauth_user_groups
  9. Creatingtableauth_user
  10. Creatingtableauth_message
  11. Creatingtabledjango_content_type
  12. Creatingtabledjango_session
  13. Creatingtabledjango_site
  14. YoujustinstalledDjango'sauthsystem,whichmeansyoudon'thaveanysuperusersdefined.--phpfensi.com
  15. Wouldyouliketocreateonenow?(yes/no):yes
  16. Username(Leaveblanktouse'root'):itchenyi
  17. E-mailaddress:itchenyi@gmail.com
  18. Password:
  19. Password(again):
  20. Superusercreatedsuccessfully.
  21. InstallingcustomSQL...
  22. Installingindexes...
  23. Nofixturesfound.

6、简单验证:

  1. [root@itchenyi-1itchenyi]#pythonmanage.pyShell
  2. Python2.6.6(r266:84292,Dec72011,20:48:22)
  3. [gcc4.4.620110731(RedHat4.4.6-3)]onlinux2
  4. Type"help","copyright","credits"or"license"formoreinformation.
  5. (InteractiveConsole)
  6. >>>importMySQLdb
  7. >>>db=MySQLdb.connect(user='itchenyi',db='itchenyi_db',passwd='yourpassword'
  8. ,host='localhost')
  9. >>>

广告内容

配置Django使用MySQL数据库的例子 配置Django使用MySQL数据库的例子 配置Django使用MySQL数据库的例子

相关阅读

热门评论

Seven 绯闻SEO Seven 绯闻SEO

绯闻SEO,一个专注中小企业网站优化的SEO爱好者

总篇数179

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法