一、Django安装
Django是python编程语言驱动的一个开源模型、视图、控制器(MVC)风格的web应用程序框架,其最初的开发者是Adrian和Simon。开发背景是为了快速建立新闻站点,开源时间是2005年。
官方站点:https://www.djangoproject.com
官方文档:https://docs.djangoproject.com/en/1.9/
目前Django的最新版为1.10.5,对应的python版本为2.7以及3.4以上版本,本文安装的Django版本为1.9.12,python版本为2.7(编译安装),系统版本为CentOS6.7。
1、Django安装
1
2
3
4
|
# wget # tar xf Django-1.9.12.tar.gz -C /opt/project/ # cd /opt/project/Django-1.9.12/ # python2.7 setup.py install |
2、Django建立项目
1
2
3
4
5
6
7
8
9
10
11
12
|
建立名为mysite的项目 [root@node2 project] # django-admin startproject mysite
[root@node2 project] # cd mysite/
[root@node2 mysite] # tree .
. ├── manage.py Django的管理工具,可用来创建应用程序 └── mysite ├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
|
3、运行项目及常见错误
1
2
3
4
5
6
7
8
9
|
在虚拟机中实验须指出监听地址及端口 [root@node2 mysite] # python2.7 manage.py runserver 0.0.0.0:8000
出现以下报错 File "/usr/local/python27/lib/python2.7/site-packages/Django-1.9.12-py2.7.egg/django/db/backends/sqlite3/base.py" , line 39, in <module>
raise ImproperlyConfigured( "Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3
未安装sqlite3模块,安装该模块,并重新安装python |
sqlite3模块下载地址:https://sqlite.org/download.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
编译安装sqlite3模块 [root@node2 ~] # tar xf sqlite-snapshot-201701121910.tar.gz
[root@node2 ~] # cd sqlite-snapshot-201701121910
# ./configure --prefix=/usr/local/sqlite3 # make && make install 重新编译安装python,加载sqlite3模块 编辑setup.py,添加sqlite的include信息 [root@node2 ~] # cd Python-2.7.12
将 /usr/local/sqlite3/include 加入setup.py中
[root@node2 Python-2.7.12] # vim setup.py
sqlite_inc_paths = [ '/usr/include' ,
'/usr/include/sqlite' ,
'/usr/include/sqlite3' ,
'/usr/local/include' ,
'/usr/local/include/sqlite' ,
'/usr/local/include/sqlite3' ,
'/usr/local/sqlite3/include' ,
]
[root@node2 Python-2.7.12] # ./configure --prefix=/usr/local/python27
[root@node2 Python-2.7.12] # make && make install
|
测试模块是否安装成功
1
2
3
4
5
6
7
8
9
10
11
|
[root@node2 ~] # ipython
Python 2.7.12 (default, Jan 16 2017, 22:34:48) Type "copyright" , "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object' , use 'object??' for extra details.
In [1]: import sqlite3
|
测试Django
1
2
|
[root@node2 ~] # cd /opt/project/mysite/
[root@node2 mysite] # python2.7 manage.py runserver 192.168.8.230:8000
|
出现报错
1
2
|
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
|
根据报错提示迁移模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@node2 mysite] # python2.7 manage.py migrate
Operations to perform: Apply all migrations: admin, contenttypes, auth, sessions
Running migrations: Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying sessions.0001_initial... OK
|
继续测试
1
|
[root@node2 mysite] # python2.7 manage.py runserver 0.0.0.0:8000
|
浏览器出现以下信息
出现这种情况需要在mysite/settings.py中修改监听地址为本机ip
1
2
|
[root@node2 mysite] # vim mysite/settings.py
ALLOWED_HOSTS = [ '192.168.8.230' ,]
|
再次测试
1
|
[root@node2 mysite] # python2.7 manage.py runserver 0.0.0.0:8000
|
启动成功,不过本文启动的是Django内置的server,仅用于开发测试,不做实际使用。同时,更改python文件,Django会自动作reload。
本文转自 元婴期 51CTO博客,原文链接:http://blog.51cto.com/jiayimeng/1892391