一、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安装
# 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建立项目
建立名为mysite的项目 [root@node2 project]# django-admin startproject mysite [root@node2 project]# cd mysite/ [root@node2 mysite]# tree . . ├── manage.py └── mysite ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
3、运行项目
在虚拟机中实验须指出监听地址及端口 [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
编译安装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
测试模块是否安装成功
[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
[root@node2 ~]# cd /opt/project/mysite/ [root@node2 mysite]# python2.7 manage.py runserver 192.168.8.230:8000
出现报错
You have unapplied migrations; your app may not work properly until they are applied. Run ‘python manage.py migrate‘ to apply them.
根据报错提示迁移模块
[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
继续测试
[root@node2 mysite]# python2.7 manage.py runserver 0.0.0.0:8000
浏览器出现以下信息
出现这种情况需要在mysite/settings.py中修改监听地址为本机ip
[root@node2 mysite]# vim mysite/settings.py ALLOWED_HOSTS = [‘192.168.8.230‘,]
再次测试
[root@node2 mysite]# python2.7 manage.py runserver 0.0.0.0:8000
启动成功,不过本文启动的是Django内置的server,仅用于开发测试,不做实际使用。同时,更改python文件,Django会自动作reload。
本文出自 “linux启航” 博客,请务必保留此出处http://jiayimeng.blog.51cto.com/10604001/1892391
原文地址:http://jiayimeng.blog.51cto.com/10604001/1892391