百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> 配置Django使用MySQL数据库的例子
分享文章到:

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

发布时间:01/15 来源: 浏览: 关键词:
Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架了,下面我们就来介绍这款框架配置Django使用MySQL数据库的例子了。


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

[root@itchenyi-1 Django-1.3.3]# yum install mysql-server mysql-devel
[root@itchenyi-1 Django-1.3.3]# yum install MySQL-python

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

[root@itchenyi-1 Django-1.3.3]# service mysqld start
[root@itchenyi-1 Django-1.3.3]# mysql -u root -p

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database itchenyi_db;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL ON itchenyi_db.* TO 'itchenyi'@'localhost' IDENTIFIED BY 'your password';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
3、create a django project:

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


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

[root@itchenyi-1 Django-1.3.3]# vi itchenyi/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'itchenyi_db',                      # Or path to database file if using sqlite3.
        'USER': 'itchenyi',                      # Not used with sqlite3.
        'PASSWORD': 'your password',                  # Not used with sqlite3.
        'host': '',                      # set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

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

[root@itchenyi-1 Django-1.3.3]# cd itchenyi/
[root@itchenyi-1 itchenyi]# python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'root'): itchenyi
E-mail address: itchenyi@gmail.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found.

6、简单验证:

[root@itchenyi-1 itchenyi]# python manage.py Shell
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[gcc 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import MySQLdb
>>> db = MySQLdb.connect(user='itchenyi',db='itchenyi_db',passwd='your password'
,host='localhost')
>>>

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有8人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板