日期:2014-05-16 浏览次数:20359 次
系统:Ubuntu12.04
Django 版本:1.5
确定环境已经搭建好,命令行中输入:
jiangge@ubuntu:~$ python -c "import django; print(django.get_version())" 1.5
1.5是Django的版本,本文基于1.5写成.
创建一个项目: 在命令行输入:
jiangge@ubuntu:~$ django-admin.py startproject mysite
此命令创建一个一个Django项目,tree一下mysite目录,可以看到:
jiangge@ubuntu:~$ tree mysite
mysite
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
启动开发者服务器:
切换到外面的mysite目录(Change into the outer mysite directory),然后在命令行输入:
jiangge@ubuntu:~$ cd mysite
jiangge@ubuntu:~/mysite$ python manage.py runserver
会看到命令行有如下信息,则表示启动成功:
Validating models...
0 errors found
March 09, 2013 - 22:48:24
Django version 1.5, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
在浏览器中输入:
http://127.0.0.1:8000/
数据库配置:
编辑mysite目录下的settings.py文件
jiangge@ubuntu:~/mysite$ vim mysite/settings.py
填写数据库引荐,数据库名,用户名,密码,主机,端口号:
默认已经创建数据库:
mysql>CREATE DATABASE mysite DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
12 DATABASES = { 13 'default': { 14 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 15 'NAME': 'mysite', #mysql> CREATE DATABASE mysite DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 17 'USER': 'root', 18 'PASSWORD': '123456', 19 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 20 'PORT': '', # Set to empty string for default. 21 } 22 }