Python Web框架之Django初探(一)
Python Web框架之Django初探
Django是一個開放源代碼的Web應用框架,由Python寫成。采用了MVC的框架模式,即模型M,視圖V和控制器C。它最初是被開發來用於管理勞倫斯出版集團旗下的一些以新聞內容為主的網站的,即是CMS(內容管理系統)軟件。並於2005年7月在BSD許可證下發布。這套框架是以比利時的吉普賽爵士吉他手Django Reinhardt來命名的。
Django可以運行在Apache,Nginx上,也可以運行在支持WSGI,FastCGI的服務器上。支持多種數據庫,已經支持Postgresql,MySql, Sqlite3,Oracle。Google App Engine也支持Django的某些部分,國內支持的平臺有(SAE)Sina App Engine,(BAE)百度應用引擎。
Django 項目是一個python定制框架,它源自一個在線新聞 Web 站點,於 2005 年以開源的形式被釋放出來。
Django 框架的核心組件有:
》》用於創建模型的對象關系映射
》》為最終用戶設計的完美管理界面
》》一流的 URL 設計
》》設計者友好的模板語言
》》緩存系統
Django(發音:[`dɡ]) 是用python語言寫的開源web開發框架(open source web framework),它鼓勵快速開發,並遵循MVC設計。Django遵守BSD版權,初次發布於2005年7月, 並於2008年9月發布了第一個正式版本1.0 。Django 根據比利時的爵士音樂家Django Reinhardt命名,他是一個吉普賽人,主要以演奏吉它為主,還演奏過小提琴等。由於Django在近年來的迅速發展,應用越來越廣泛,被著名IT開發雜誌SD Times評選為2013 SD Times 100,位列“API、庫和框架”分類第6位,被認為是該領域的佼佼者。
================================================================================================
一、Linux平臺搭建Django環境
================================================================================================
1、Linux系統環境
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[[email protected] ~]# uname -a
Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# uname -r
3.10.0-514.el7.x86_64
[[email protected] ~]# uname -m
x86_64
2、安裝python
python2.7.5已經搭建好了
[[email protected] ~]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
2、安裝pip
[[email protected] ~]# pip -V
pip 9.0.1 from /usr/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
3、安裝setuptools
[[email protected] ~]# yum -y install setuptools
4、安裝Django環境
[[email protected] ~]# easy_install django
或者[[email protected] ~]# pip install django
5、測試django是否安裝成功
[[email protected] setuptools-36.5.0]# django-admin --version
1.11.5
或者
[[email protected] setuptools-36.5.0]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
‘1.11.5‘
【更新地址】http://doublelinux.blog.51cto.com/12300166/1965832
================================================================================================
二、django使用
================================================================================================
1、使用Django創建一個名為helloworld的項目
[[email protected] setuptools-36.5.0]# cd /home/qiu/
[[email protected] qiu]# django-admin startproject helloworld
[[email protected] qiu]# ls
helloworld
2、運行項目helloworld
[[email protected] qiu]# cd helloworld/
[[email protected] helloworld]# ls
helloworld manage.py
[[email protected] helloworld]# python manage.py runserver 192.168.255.128:8000
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run ‘python manage.py migrate‘ to apply them.
September 16, 2017 - 06:27:28
Django version 1.11.5, using settings ‘helloworld.settings‘
Starting development server at http://192.168.255.128:8000/
Quit the server with CONTROL-C.
3、防火墻和selinux設置
[[email protected] hello]# systemctl stop firewalld
[[email protected] hello]# setenforce 0
4、瀏覽器中訪問
出現問題
5、解決問題
[[email protected] qiu]# cd /home/qiu/helloworld/
[[email protected] helloworld]# ls
db.sqlite3 helloworld manage.py
[[email protected] helloworld]# cd helloworld/
[[email protected] helloworld]# ls
__init__.py __init__.pyc settings.py settings.pyc urls.py urls.pyc wsgi.py wsgi.pyc
[[email protected] helloworld]# vim settings.py
將ALLOWED_HOSTS = []修改為ALLOWED_HOSTS = [‘*‘]
6、訪問正常
================================================================================================
三、
================================================================================================
本文出自 “doublelinux” 博客,謝絕轉載!
Python Web框架之Django初探(一)