安装 python setup.py install
使用
1.创建工程
django-admin.py startproject Data_Collet_Center
2.运行开发server
python manage.pyrunserver 0.0.0.0:8000
3.创建应用
python manage.py startapp DCC
4.配置
Data_collect_Center/settings.py
配置DB:
‘ENGINE‘: ‘django.db.backends.sqlite3‘, # Add ‘postgresql_psycopg2‘, ‘mysql‘, ‘sqlite3‘ or ‘oracle‘.
‘NAME‘: ‘my.db‘,
添加APP:
INSTALLED_APPS = () 中加入刚添加的DCC
Data_Collect_Center/urls.py
函数末尾添加:
url(r‘^index/$‘,‘DCC.views.index‘),
DCC/views.py
from django.http import HttpResponse
def index(req):
return HttpResponse("<h1>hello world!</h1>")
5.用apache和wsgi部署django
5.1 django start project
django-admin.py startproject Data_Collect_Center
5.2 django应用目录tree
/opt/Data_Collect_Center/
|-- DCC
| |-- admin.py
| |-- admin.pyc
| |-- __init__.py
| |-- __init__.pyc
| |-- models.py
| |-- models.pyc
| |-- tests.py
| |-- views.py
| `-- views.pyc
|-- django.wsgi
|-- manage.py
`-- Data_Collect_Center
|-- __init__.py
|-- __init__.pyc
|-- settings.py
|-- settings.pyc
|-- urls.py
|-- urls.pyc
|-- wsgi.py
`-- wsgi.pyc
5.3 apache及mod_wsgi安装
yum -y install httpd mod_wsgi
5.4 vim /etc/httpd/conf.d/wsgi.conf
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / "/opt/Data_Collect_Center/django.wsgi"
<Directory "/opt/Data_Collect_Center">
Order Deny,Allow
Allow from all
</Directory>
5.5 编辑django.wsgi
cat /opt/Data_Collect_Center/django.wsgi
# -*- coding: utf-8 -*-
import os
import sys
os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘Data_Collect_Center.settings‘
os.environ[‘PYTHON_EGG_CACHE‘] = ‘/tmp/.python-eggs‘
current_dir = os.path.dirname(__file__)
if current_dir not in sys.path: sys.path.append(current_dir)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
5.6 重启httpd即可
6、django连接mysql配置
6.1 安装MySQLdb
yum -y install MySQL-python
6.2 配置settings.py的DATABASES
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘Data_Collect_Center‘,
‘USER‘: ‘mysql1‘,
‘PASSWORD‘: ‘123456‘,
‘HOST‘: ‘X.X.X.X‘,
‘PORT‘: ‘3306‘,
}
6.3 mysql安装及配置
6.3.1 mysql安装
yum -y install mysql-server mysql
6.3.2 mysql配置(创建数据库,用户名及密码)
mysql> create database Data_Collect_Center;
mysql> grant all privileges on Data_Collect_Center.* to [email protected]"%" identified by "123456";
6.3.3 编写模型 DDC/models.py
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
6.3.4 验证模型的有效性
python manage.py validate
6.3.5 模型没问题后,运行下面命令来生成CREATE TABLE语句
CREATE TABLE `DCC_publisher` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(30) NOT NULL,
`address` varchar(50) NOT NULL,
`city` varchar(60) NOT NULL,
`state_province` varchar(30) NOT NULL,
`country` varchar(50) NOT NULL,
`website` varchar(200) NOT NULL
)
;
CREATE TABLE `DCC_author` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(40) NOT NULL,
`email` varchar(75) NOT NULL
)
;
CREATE TABLE `DCC_book_authors` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`book_id` integer NOT NULL,
`author_id` integer NOT NULL,
UNIQUE (`book_id`, `author_id`)
)
;
ALTER TABLE `DCC_book_authors` ADD CONSTRAINT `author_id_refs_id_274fbb64` FOREIGN KEY (`author_id`) REFERENCES `DCC_author` (`id`);
CREATE TABLE `DCC_book` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`title` varchar(100) NOT NULL,
`publisher_id` integer NOT NULL,
`publication_date` date NOT NULL
)
;
ALTER TABLE `DCC_book` ADD CONSTRAINT `publisher_id_refs_id_5bbe8bc8` FOREIGN KEY (`publisher_id`) REFERENCES `DCC_publisher` (`id`);
ALTER TABLE `DCC_book_authors` ADD CONSTRAINT `book_id_refs_id_7172d865` FOREIGN KEY (`book_id`) REFERENCES `DCC_book` (`id`);
CREATE INDEX `DCC_book_authors_36c249d7` ON `DCC_book_authors` (`book_id`);
CREATE INDEX `DCC_book_authors_e969df21` ON `DCC_book_authors` (`author_id`);
CREATE INDEX `DCC_book_81b79144` ON `DCC_book` (`publisher_id`);
COMMIT;
6.3.6 同步数据库
python manage.py syncdb
6.3.7 检查
mysql -umysql1 -h 172.23.178.43 -p123456
web开发框架Django笔记整理,布布扣,bubuko.com