Django做为python最热门的web框架,今天尝试部署。像其他的框架一样,我们也是主要学习他的命名规范和API。通过这些来完成一个网络框架的使用。使用Django的前提是对python的熟悉。
Django的安装:
我选择的从源码安装,通过github来获取源代码来进行安装。
git clone https://github.com/django/django.git
root权限下:
python setup.py install
接下来查看Django安装是否成功哦。
import django
django.VERSION
输出:
(1, 6, 0, 'alpha', 0)
安装数据库:
Django的web应用一般都需要一种数据库来支持,再使用数据库前需要安装一个数据库驱动。
支持以下四种数据库:
PostgreSQL(http://www.postgresql.org)
SQLite3 (http://www.aqlite.org)
MySQL(http://www.mysql.com)
Oracle(http://www.oracle.com)
现在比较主流的数据库是mysql,之前接触过sqlite比较轻量级别的数据库,这里就是用mysql最为以后的开发环境。
通过源来安装Mysql,安装完成后设置root的密码。
mysqladmin -uroot -p -h localhost password passwd
如果有需要,可以修改 /etc/mysql/my.cnf 对 MySQL 进行配置。
然后启动 MySQL:
然后启动 MySQL:
lrqrun@lrqrun:/etc/init.d/mysql start
测试是否安装成功:
lrqrun@lrqrun:~/work/Django$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.5.28-0ubuntu0.12.04.3 (Ubuntu)
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Django的部署:
创建你的工作目录:
/home/username/djcode
转到你创建的目录,运行命令django-admin.py startproject mysite 这样在你的当前目录下创建一个目录 mysite
他包含以下文件:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
*__init__.py : 让python把该目录当成一个开发包(即一个模块)所需的文件。这是一个空文件,一般你不需要修改它。
*manage.py :一种命令行工具,允许你以多种方式与该Django项目进行交互。键入python manage.py help,看一下它能做什么。你不需要编辑这个文件。
Usage: manage.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Print traceback on exception
--version show program's version number and exit
-h, --help show this help message and exit
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[django]
cleanup
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
runfcgi
shell
sql
sqlall
sqlclear
sqlcustom
sqlflush
sqlindexes
sqlinitialdata
sqlsequencereset
startapp
startproject
syncdb
test
testserver
validate
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
*settings.py : 该Django项目的设置或配置。查看并理解这个文件中可用的设置类型及其默认值。
*urls.py : Django项目的URL设置。可视其为你的Django网站的目录。目前它是空的。
*wsgi: Django项目WSGI WEB服务框架的配置文件。
运行开发服务器:
在当前目录下运行python manage.py runserver 启动服务器。
默认情况下runserver命令在8000端口启动开发服务器,且仅监听本地连接。要想更改服务器端口的话,可将端口作为命令行参数传入:
python manage.py runserver 8080
Validating models...
0 errors found
December 15, 2012 - 00:09:42
Django version 1.6, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8080/
Quit the server with CONTROL-C.
当然也可一通过指定一个IP地址来监听任意的网络接口。
python manage.py runserver 0.0.0.0:8080