Centos 7.5 python3.6.1Django 環境設置

一、Django 概述

Django 中提供了開發網站經常用到的模塊,常見的代碼都爲你寫好了,通過減少重複的代碼,Django 使你能夠專注於 web 應用上有 趣的關鍵性的東西。爲了達到這個目標,Django 提供了通用Web開發模式的高度抽象,提供了頻繁進行的編程作業的快速解決方法,以及爲“如何解決問題”提供了清晰明瞭的約定。Django的理念是DRY(Don't Repeat Yourself)來鼓勵快速開發!

Django全貌

urls.py
網址入口,關聯到對應的views.py中的一個函數(或者generic類),訪問網址就對應一個函數。

views.py
處理用戶發出的請求,從urls.py中對應過來, 通過渲染templates中的網頁可以將顯示內容,比如登陸後的用戶名,用戶請求的數據,輸出到網頁。

models.py
與數據庫操作相關,存入或讀取數據時用到這個,當然用不到數據庫的時候 你可以不使用。

forms.py
表單,用戶在瀏覽器上輸入數據提交,對數據的驗證工作以及輸入框的生成等工作,當然你也可以不使用。

templates 文件夾
views.py 中的函數渲染templates中的Html模板,得到動態內容的網頁,當然可以用緩存來提高速度。

admin.py
後臺,可以用很少量的代碼就擁有一個強大的後臺。

settings.py
Django 的設置,配置文件,比如 DEBUG 的開關,靜態文件的位置等。

二、系統環境設置

1、修改系統字符集

echo 'LANG="en_GB.utf8"' > /etc/sysconfig/i18n && cat /etc/sysconfig/i18n

2、修改YUM源

參考
Centos防火牆&SELINUX關閉及yum源配置

三、python安裝

1、安裝python相關依賴

yum -y install openssl-devel readline-devel unzip gcc

2、下載python源碼包

wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

3、編譯安裝python

tar -xvf Python-3.6.1.tgz 
cd Python-3.6.1
./configure --prefix=/usr/local/python36
make && make install

查看所安裝的工具

$ ll /usr/local/python36/bin/
總用量 24660
lrwxrwxrwx 1 root root        8 12月 12 15:36 2to3 -> 2to3-3.6
-rwxr-xr-x 1 root root      110 12月 12 15:36 2to3-3.6
-rwxr-xr-x 1 root root      251 12月 12 15:36 easy_install-3.6
lrwxrwxrwx 1 root root        7 12月 12 15:36 idle3 -> idle3.6
-rwxr-xr-x 1 root root      108 12月 12 15:36 idle3.6
-rwxr-xr-x 1 root root      223 12月 12 15:36 pip3
-rwxr-xr-x 1 root root      223 12月 12 15:36 pip3.6
lrwxrwxrwx 1 root root        8 12月 12 15:36 pydoc3 -> pydoc3.6
-rwxr-xr-x 1 root root       93 12月 12 15:36 pydoc3.6
lrwxrwxrwx 1 root root        9 12月 12 15:36 python3 -> python3.6
-rwxr-xr-x 2 root root 12603760 12月 12 15:35 python3.6
lrwxrwxrwx 1 root root       17 12月 12 15:36 python3.6-config -> python3.6m-config
-rwxr-xr-x 2 root root 12603760 12月 12 15:35 python3.6m
-rwxr-xr-x 1 root root     3106 12月 12 15:36 python3.6m-config
lrwxrwxrwx 1 root root       16 12月 12 15:36 python3-config -> python3.6-config
lrwxrwxrwx 1 root root       10 12月 12 15:36 pyvenv -> pyvenv-3.6
-rwxr-xr-x 1 root root      450 12月 12 15:36 pyvenv-3.6
-rwxr-xr-x 1 root root      230 12月 12 15:39 virtualenv

4、配置pip  

vim /etc/pip.conf
[global]
index-url = http://pypi.douban.com/simple/ 
trusted-host=pypi.douban.com
[list]
format=columns

/usr/local/python36/bin/pip3 list
Package    Version
---------- -------
pip        9.0.1  
setuptools 28.8.0 

5、安裝virtualenv

/usr/local/python36/bin/pip3 install virtualenv

執行結果

Collecting virtualenv
  Downloading http://pypi.doubanio.com/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 983kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

四、安裝django

1、初始化env環境

創建一個普通用戶

useradd devops
passwd devops

切換到普通devops

su - devops
/usr/local/python36/bin/virtualenv ./python36env
Using base prefix '/usr/local/python36'
New python executable in /home/devops/python36env/bin/python3.6
Also creating executable in /home/devops/python36env/bin/python
Installing setuptools, pip, wheel...done.

進入虛擬環境

source python36env/bin/activate
(python36env) [devops@devops-python36 ~]$ pip list
Package    Version
---------- -------
pip        9.0.1  
setuptools 38.2.4 
wheel      0.30.0 

2、django版本選擇

Django 1.5.x 支持 Python 2.6.5 Python 2.7, Python 3.2 和 3.3.
Django 1.6.x 支持 Python 2.6.X, 2.7.X, 3.2.X 和 3.3.X
Django 1.7.x 支持 Python 2.7, 3.2, 3.3, 和 3.4 (注意:Python 2.6 不支持了)
Django 1.8.x 支持 Python 2.7, 3.2, 3.3, 3.4 和 3.5.  (長期支持版本 LTS)
Django 1.9.x 支持 Python 2.7, 3.4 和 3.5. 不支持 3.3 了
Django 1.10.x 支持 Python 2.7, 3.4 和 3.5. 
Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6(長期支持版本 LTS) 最後一個支持 Python 2.7 的版本
Django 2.0.x 支持 Python 3.4, 3.5 和 3.6 (注意,不再支持 Python 2)

3、安裝django

pip install "django>=1.11

(python36env) [devops@devops-python36 ~]$ pip install "django>=1.11,<=1.12"
Collecting django<=1.12,>=1.11
  Downloading http://pypi.doubanio.com/packages/7e/36/5266e0c51ee9b953d60ea8ea1fea10e268b1368f9c0ad08e2ff76ee9c1b5/Django-1.11.8-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 1.9MB/s 
Collecting pytz (from django<=1.12,>=1.11)
  Downloading http://pypi.doubanio.com/packages/a3/7f/e7d1acbd433b929168a4fb4182a2ff3c33653717195a26c1de099ad1ef29/pytz-2017.3-py2.py3-none-any.whl (511kB)
    100% |████████████████████████████████| 512kB 1.4MB/s 
Installing collected packages: pytz, django
Successfully installed django-1.11.8 pytz-2017.3

4、mysql安裝

數據庫安裝參考
Centos7.5安裝mysql5.7.24二進制包方式部署

5、安裝pip pymysql

(python36env) [devops@devops-python36 ~]$ pip install pymysql
Collecting pymysql
  Downloading http://pypi.doubanio.com/packages/c6/42/c54c280d8418039bd2f61284f99cb6d9e0eae80383fc72ceb6eac67855fe/PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 2.6MB/s 
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11

(python36env) [devops@devops-python36 ~]$ pip list
Package    Version
---------- -------
Django     1.11.8 
pip        9.0.1  
PyMySQL    0.7.11 
pytz       2017.3 
setuptools 38.2.4 
wheel      0.30.0 

五、配置vim

yum -y install vim

vim ~/.vimrc
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set fileformat=unix 
set nobomb
set ff=unix
set ambiwidth=double 
set fileencodings=utf-8,ucs-bom,cp936
syntax on
filetype plugin on 
set nocompatible
set completeopt=preview
set ai 
set hls
set nu
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章