您的位置:首页 > 编程语言 > Python开发

python web框架Django之基础第一篇

2017-01-16 20:10 711 查看
一、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    Django的管理工具,可用来创建应用程序
└── 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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web 框架 python