django生產環境搭建(uWSGI+django+nginx+python+MySQL)

一、系統環境,必要知識

#cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) #uname -r 3.10.0-862.3.2.el7.x86_64

暫時關閉防禦牆,關閉selinux:css

#systemctl stop firewalld.service #setenforce 0 #getenforce Permissive

準備知識:html

django:一個基於python的開源web框架。node

uWSGI:一個基於自有的uwsgi協議,wsgi協議和http服務協議的web網關python

nginx:高性能的代理web服務器mysql

wsgi.py:django項目自帶的wsgi接口文件(位於:項目/項目名/wsgi.py)jquery

整個項目流程:linux

首先客戶端經過瀏覽器訪問服務器資源;nginx做爲對外服務的端口(80),nginx接收到客戶端http請求後會解包分析,若是是靜態文件就去配置的靜態文件中查找資源並返回給客戶端,若是是動態資源,nginx就經過配置文件將請求傳遞給uwsgi處理,並轉發給uwsgi,wsgi根據請求調用django工程的文件和函數,處理後django將返回值交給wsgi,wsgi將返回值進行打包,轉發給uWSGI,uWSGI接收到數據後轉發給nginx,最終返回給客戶端。nginx

二、安裝python3.6.5

(1)安裝python依賴包git

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel

(2)安裝pythongithub

#wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
#mkdir -p /usr/local/python356 #tar zxvf Python-3.6.5.tgz #cd Python-3.6.5 #./configure --prefix=/usr/local/python356 #make #make install #ln -s /usr/local/python356/bin/python3 /usr/local/bin/python3 #ln -s /usr/local/python356/bin/pip3 /usr/local/bin/pip3 #pip3 install --upgrade pip #更新pip #pip3 install ipython  #安裝ipython方便調試

測試安裝是否正常:

#python3 -V Python 3.6.5 #pip3 -V pip 10.0.1 from /usr/local/python356/lib/python3.6/site-packages/pip (python 3.6)

三、安裝Django

#pip3 install django

#ln -s /usr/local/python356/bin/django-admin /usr/local/bin/django-admin

建立項目:

#django-admin startproject mysite

建立app:

#cd mysite/
#django-admin startapp app01

在項目路徑下建立模板文件目錄(templates)和靜態文件目錄(static),後面會用到

# cd mysite/ # mkdir templates static

配置setting.py文件,配置容許訪問主機名、將APP加入到項目、添加模板搜索路徑

# vim mysite/settings.py
 ALLOWED_HOSTS = ["*"]  #容許本機全部地址訪問
 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01',      #加入app名
] TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,'templates')],  #加入模板搜索路徑
...... ]

在views.py文件中建立視圖函數

# vim app01/views.py
from django.shortcuts import render def index(request): return render(request,"app01/index.html")

在templates目錄中新建測試模板文件

# mkdir -p templates/app01 # vim templates/app01/index.html

<h1>test django server</h1>

在urls.py文件中創建路由

# vim mysite/urls.py
from django.contrib import admin from django.urls import path,re_path  #添加
from app01 import views  #添加
 urlpatterns = [ path('admin/', admin.site.urls), re_path('^$',views.index,name='index'),  #添加的路由
]

檢查配置是否有錯誤:

# python3 manage.py check
System check identified no issues (0 silenced).

運行測試服務器,測試服務是否正常

# python3 manage.py runserver 0:8000 #在瀏覽器中測試是否正常顯示網頁內容 #注意防火牆和selinux是否關閉或容許相關端口訪問 #curl 127.0.0.1:8000
<h1>test django server</h1>

四、安裝uWSGI

使用python的pip工具包安裝:

#pip3 install uwsgi #ln -s /usr/local/python356/bin/uwsgi /usr/local/bin/uwsgi #創建軟連接 #uwsgi --version #檢查安裝成功 2.0.17

先在項目目錄下創建一個測試文件:

# vim foobar.py
def application(env,start_response): start_response('200 ok',[('Content-Type','text/html')]) return [b"Hello World"]

而後採用測試腳本方式啓動服務:

# uwsgi --http :9000 --wsgi-file foobar.py 
#經過瀏覽器訪問http://ip:9000能看到hello world說明成功
#而後中止服務

接下來配置uwsgi部署django服務:

#運行測試服務,--http指定服務端口,--chdir指定django項目目錄,--wsgi-file指定項目中的wsgi.py文件
# uwsgi --http :9000 --chdir /opt/mysite/ --wsgi-file mysite/wsgi.py --master --ocesses 4 --threads 2 --stats 127.0.0.1:9191

#經過瀏覽器http://ip:9000
#如能訪問到diango模板中的html文件中的內容就算成功
#個人上面django模板中配置的內容是
test django server

創建uWSGI的配置文件,在django項目中創建uwsgi.ini文件,並配置好django項目,此處使用socket連接,後面經過nginx反向代理

#注意此處的配置指定用戶nginx必須存在

[uwsgi] socket = 127.0.0.1:9090 chdir = /djproject/mysite module = mysite.wsgi #這裏填的是相對路徑 master = true processes = 2 threads = 2 max-requests = 2000 vacuum = true daemonize = /djproject/mysite/uwsgi.log stats = 127.0.0.1:9001 post-buffering = 65535 buffer-size = 65535 harakiri-verbose = true harakiri = 300 uid = nginx pidfile = /djproject/mysite/uwsgi.pid

參數說明:

socket:使用套接字連接

http:經過http加端口的方式

wsgi-file:指定加載的wsgi文件

chdir:指定工程的絕對路徑,如Django的項目路徑

module:指定web應用的入口模塊,如Django項目下的wsgi.py接口文件

master:啓動主進程

processes:啓動進程數

threads:啓動線程數

max-requests:最大請求數

daemonize:指定uWSGI日誌文件路徑

stats:指定狀態查詢端口,如:127.0.0.1:9001

wsgi-file:指定啓動的文件

post-buffering:設置緩衝區

buffer-size:設置緩衝區文件大小

harakiri-verbose:設置超時true爲開啓

harakiri:設置超時時間

uid、gid:設置用戶和組

pidfile:指定啓動時的pid文件路徑

vacuum = true #當服務器退出的時候自動刪除unix socket文件和pid文件

uwsgi信號控制:

HUP     :優雅的從新加載全部進程和配置,同(--reload)同樣

TERM  :強制從新加載主進程和配置

 INT  :當即殺死整個uWSGI;同於:--stop

QUIT  :當即殺死真個uWSGI

從新啓動實例:

#kill -HUP `cat /tmp/project-mast.pid`

#uwsgi --reload /tmp/project-master.pid

還能夠在python程序中使用uwsgi.reload()從新加載

中止服務器:

#uwsgi --stop /tmp/project-master.pid

#kill -INT `cat /tmp/project-master.pid`

編寫啓動腳本:

#vim /etc/init.d/uwsgi 

#!/bin/bash #this is uwsgi server script . /etc/init.d/functions uwsgi=/usr/local/bin/uwsgi uwsgi_pid=/djangoproject/mysite/uwsig.pid uwsgi_conf=/djangoproject/mysite/uwsigconf.ini uwsgi_pn=`ps -ef|grep -v "grep"|grep -c "uwsgi"` ERVAL=0 start(){ $uwsgi --ini $uwsgi_conf >& /dev/unll ERVAL=$?
    if [ $ERVAL -eq 0 ];then action "uwsgid starting ..." /bin/true
    else action "uwsgid start is error" /bin/false
    fi } stop(){ $uwsgi --stop $uwsgi_pid >& /dev/null ERVAL=$?
    if [ $ERVAL -eq 0 ];then action "uwsgid stoping ..." /bin/true
    else action "uwsgid stop is error" /bin/false
    fi } case "$1" in start) if [ $uwsgi_pn -ge 5 ];then action "uwsgi is running!" /bin/false
        else start ERVAL=0
        fi ;; stop) if [ $uwsgi_pn -ge 5 ];then stop ERVAL=0
        else action "uwsgi no running!" /bin/false
        fi ;; reload) if [ $uwsgi_pn -ge 5 ];then $uwsgi --reload $uwsgi_pid >&/dev/null ERVAL=$? [ $ERVAL -eq 0 ] && action "uwsgi reloading ..." /bin/true
        else action "uwsgi reload error" /bin/false
        fi ;; restart) stop sleep 2 start ;; *) echo $"Usage: $0 {start|stop|restart|reload|status}" ERVAL=2
esac exit $ERVAL

 centOS7 system系統服務腳本:

#cat /usr/lib/systemd/system/uwsgi.service [Unit] Description=uwsgi service After=network.target [Service] Type=forking PIDFile=/run/uwsgi.pid ExecStartPre=/usr/bin/rm -f /run/uwsgi.pid ExecStart=/usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target

centOS7創建system啓動腳本後須要加載下:

systemctl daemon-reload

測試服務:

#uwsgi --ini /djproject/mysite/uwsgi.ini #啓動服務 #ps -ef|grep "uwsgi" #查看進程 root 103596      1  2 16:02 ?        00:00:00 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini root 103598 103596  0 16:02 ?        00:00:00 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini root 103599 103596  0 16:02 ?        00:00:00 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini #netstat -lntp|grep "uwsgi" tcp 0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      103596/uwsgi tcp 0      0 127.0.0.1:9001          0.0.0.0:*               LISTEN      103596/uwsgi

看上面進程是少了一個主進程,經過systemctl 查看就清楚了:

#systemctl status -l uwsgi.service ● uwsgi.service - uwsgi service Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; disabled; vendor preset: disabled) Active: active (running) since 五 2018-05-25 16:02:06 CST; 4min 14s ago Process: 103593 ExecStart=/usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini (code=exited, status=0/SUCCESS) Process: 103591 ExecStartPre=/usr/bin/rm -f /run/uwsgi.pid (code=exited, status=0/SUCCESS) Main PID: 103596 (uwsgi) CGroup: /system.slice/uwsgi.service ├─103596 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini ├─103598 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini └─103599 /usr/local/python356/bin/uwsgi --ini /djproject/mysite/uwsgi.ini 5月 25 16:02:06 ZPY systemd[1]: Starting uwsgi service... 5月 25 16:02:06 ZPY uwsgi[103593]: [uWSGI] getting INI configuration from /djproject/mysite/uwsgi.ini 5月 25 16:02:06 ZPY systemd[1]: PID file /run/uwsgi.pid not readable (yet?) after start. 5月 25 16:02:06 ZPY systemd[1]: Started uwsgi service.

#經過查看uwsgi的日誌,如沒報錯,就接着下一步,部署nginx

五、安裝nginx

這裏採用yum安裝nginx:

yum -y install nginx

配置nginx反向代理uwsgi服務:

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; charset utf-8; access_log /var/log/nginx/nginx_access.log main; error_log /var/log/nginx/nginx_error.log; client_max_body_size 75M; location /static { alias /djproject/mysite/static/ckeditor/; #指定django的靜態文件 } location / { include /etc/nginx/uwsgi_params; #加載uwsgi模塊 uwsgi_pass 127.0.0.1:9090; #全部請求轉到9090端口交給uwsgi處理 } } }

 此處記錄下配置uwsgi和nginx套接字通訊的坑:

第一個,nginx日誌報錯:

connect() to unix:///tmp/uwsgi.sock failed (2: No such file or directory) while connecting to upstream ......

能夠明明uwsgi配置sock套接字通訊在/tmp目錄下明明有uwsgi.sock文件,並且/tmp目錄是777權限,爲何nginx找不到套接字文件,通過google,百度查資料終於在stackoverflow上找到答案:是由於centOS7系統中/tmp臨時目錄,使用了命名空間的臨時目錄,對於每一個服務只能訪問本身建立的臨時文件,因此咱們不該該將sock文件放入/tmp目錄,而因該放入/run目錄下

第二個,uwsgi日誌報錯:

bind(): Permission denied [core/socket.c line 230]

通過第一個錯誤折騰,終於能夠了,但放入/run目錄後,uwsgi啓動不了了,權限不夠,這個問題解決簡單,只需給/run目錄,有uwsgi服務用戶也就是uid指定的用戶有寫入權限便可,或者直接給/run目錄777的權限但這樣不安全。

#chmod o+w /run #個人解決方法

下面是uwsgi和nginx的配置:

#vim /uwsgi.ini
 [uwsgi] socket = /run/uwsgi.sock #使用sock文件 chdir = /mysite/myblog module = myblog.wsgi master = true processes = 2 threads = 2 max-requests = 2000 vacuum = true home = /mysite daemonize = /var/log/uwsgi/uwsgi.log stats = 127.0.0.1:9001 post-buffering = 65535 buffer-size = 65535 harakiri-verbose = true harakiri = 300 pidfile = /run/uwsgi.pid vacuum = true
#vim /etc/ningx/nginx.conf
... server { ...... location /static { alias /opt/mysite/static; } location / { include /etc/nginx/uwsgi_params; uwsgi_pass unix:///run/uwsgi.sock;   #指定sock文件
 } }
...

 

六、MySQL安裝配置

#下載二進制包安裝
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.32-linux2.6-x86_64.tar.gz tar zxvf mysql-5.5.32-linux2.6-x86_64.tar.gz mv mysql-5.5.32-linux2.6-x86_64 /usr/local/mysql-5.5.32 ln -s /usr/local/mysql-5.5.32 /usr/local/mysql useradd -s /sbin/nologin -M mysql mkdir /mysql/data -p chown -R mysql.mysql /mysql/data cd /usr/local/mysql #添加配置文件和啓動腳本
cp support-files/my-small.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld #初始化數據庫
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data/ echo $? #修改啓動腳本路徑
sed -i 's#^basedir=#basedir=/usr/local/mysql#g' /etc/init.d/mysqld sed -i 's#^datadir=#datadir=/mysql/data#g' /etc/init.d/mysqld chmod +x /etc/init.d/mysqld #啓動和關閉MySQL
/etc/init.d/mysqld start /etc/init.d/mysqld stop #方法2:
/usr/local/mysql/bin/msyql_safe &    #後臺啓動
mysqladmin shutdown  #優雅關閉MySQL服務 #查看運行狀態 #netstat -lntup|grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      70099/mysqld #添加系統自啓動
chkconfig --add mysqld chkconfig --level 345 mysqld on #添加環境變量
echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile source /etc/profile #修改初始化密碼
mysqladmin -uroot password '123456'
#創建一個數據庫,後面要用到 MySQL [(none)]> create database django; Query OK, 1 row affected (0.00 sec)
#centOS7下經過epel源yum安裝MySQL

# yum install epel-release # yum -y install mariadb mariadb-devel mariadb-server # systemctl enable mariadb # systemctl start mariadb# mysql -e "select version();"
+----------------+
| version()      |
+----------------+
| 5.5.60-MariaDB |
+----------------+
#創建個數據庫後面要用到
MariaDB [(none)]> create database django charset 'utf8'; MariaDB [(none)]> grant all on django.* to 'admin'@'127.0.0.1' identified by '123.com'; MariaDB [(none)]> flush privileges;

(1)配置Django連接MySQL:

在setting中,Django默認使用的是sqlite數據庫:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }

修改爲MySQL數據庫配置:

DATABASES = { 'default':{ 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django', 'USER': 'root', 'PASSWORD': '123.com', 'HOST': '127.0.0.1', 'PORT': '3306', } }

ENGINE : 指定數據庫驅動,不一樣的數據庫這個字段不一樣,下面是常見的集中數據庫的ENGINE的寫法:

django.db.backends.postgresql  # PostgreSQL 
django.db.backends.mysql       # mysql 
django.db.backends.sqlite3     # sqlite 
django.db.backends.oracle      # oracle 
NAME: 指定的數據庫名,若是是sqlite的話,就須要填數據庫文件的絕對位置
USER: 數據庫登陸的用戶名,mysql通常都是root
PASSWORD:登陸數據庫的密碼,必須是USER用戶所對應的密碼
HOST: 因爲通常的數據庫都是C/S結構的,因此得指定數據庫服務器的位置,咱們通常數據庫服務器和客戶端都是在一臺主機上面,因此通常默認都填127.0.0.1
PORT:數據庫服務器端口,mysql默認爲3306
HOST和PORT均可以不填,使用默認的配置,可是若是你有更改默認配置的話,就須要填入更改後的
配置完這,下面就須要裝python鏈接mysql數據庫的驅動程序,首先,須要安裝mysql的開發包
#yum install mysql-devel #安裝MySQL插件 #pip3 install mysqlclient #安裝MySQL驅動
#測試django配置 # python3 manage.py check

環境搭建到此就完成了,下面開始開發項目

七、編寫一個簡單的博客系統

(1)數據建模,規劃博客內容字段

#首先裝兩個包django-ckeditor、pillow分別用來添加富文本和縮略圖用

# pip3 install django-ckeditor pillow
#vim app01/models.py

from django.db import models from django.utils import timezone  #導入時間模板
from django.contrib.auth.models import User  #導入用戶
from ckeditor_uploader.fields import RichTextUploadingField  #導入富文本模板

class blog_articles(models.Model): title = models.CharField(max_length=300,verbose_name="文章標題") author = models.ForeignKey(User,on_delete=models.CASCADE,related_name="blog_posts",verbose_name="文章做者") body = RichTextUploadingField(verbose_name="文章內容") description = RichTextUploadingField(verbose_name="文章描述") publish = models.DateTimeField(default=timezone.now,verbose_name="發表時間") picture = models.ImageField(upload_to="images")  #圖片
    class Meta: ordering = ("-publish",)   #按時間倒序排列

    def __str__(self): return self.title   #顯示文章標題

(2)配置setting.py

#vim mysite/settings.py  INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', 'ckeditor',   #加入模塊到項目
    'ckeditor_uploader',  #加入模板
] LANGUAGE_CODE = 'zh-hans'   #配置中文
 TIME_ZONE = 'Asia/Shanghai'  #配置時區
 USE_I18N = True USE_L10N = True USE_TZ = False #關閉UTC時間
 STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR,"static"),]  #配置靜態文件路徑
 MEDIA_URL = '/media/'  #媒體文件路由
MEDIA_ROOT = os.path.join(BASE_DIR,"static")  #媒體文件路徑
 STATIC_ROOT = os.path.join(BASE_DIR,"static/ckeditor") #配置收集靜態文件路徑
 CKEDITOR_UPLOAD_PATH = 'uploads/'  #指定媒體上傳目錄的相對路徑
CKEDITOR_IMAGE_GACKEND = 'pillow'  #使用pillow模板啓用縮略圖功能

#具體的ckedito配置文檔:https://pypi.org/project/django-ckeditor/

(3)配置admin.py後臺

# vim app01/admin.py

from django.contrib import admin from .models import blog_articles   #導入數據
 admin.site.register(blog_articles) #加載到後臺

(4)配置url.py路由

# vim mysite/urls.py
from django.contrib import admin from django.urls import path,re_path,include from app01 import views urlpatterns = [ path('admin/', admin.site.urls), re_path('^$',views.index,name='index'), path(r'ckeditor/',include('ckeditor_uploader.urls'))  #添加ckeditor路由
]

(5)收集靜態文件,生成數據並遷移

# python3 manage.py check
# python3 manage.py collectstatic # python3 manage.py makemigrations # python3 manage.py migrate #執行此處會有一個警告信息,須要配置mysql嚴格模式,在setting.py文件中的DATABASES中的數據引擎中添加如下配置: 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", },

(5)建立後臺帳戶並驗證後臺添加文章

# python3 manage.py creatsuperuser
Unknown command: 'creatsuperuser'. Did you mean createsuperuser? Type 'manage.py help' for usage. [root@PJYC7 mysite]# python3 manage.py createsuperuser
用戶名 (leave blank to use 'root'): admin 電子郵件地址: admin@admin.com Password: Password (again): 密碼長度過短。密碼必須包含至少 8 個字符。 Bypass password validation and create user anyway? [y/N]: y Superuser created successfully. #重啓uwsgi和nginx服務 # systemctl restart uwsgi # systemctl restart nginx

(6)模板展現文章

修改views.py:

#vim app01/views.py 
from django.shortcuts import render,get_object_or_404 from .models import blog_articles def index(request): #主頁 blogs = blog_articles.objects.all() return render(request,"app01/index.html",{"blogs":blogs}) def center(request,article_id): #詳情頁 article = get_object_or_404(blog_articles,id=article_id) pub = article.publish return render(request,"app01/content.html",{"article":article,"publish":pub})

創建模板文件:index.html,content.html

# vim templates/app01/index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}我的博客{% endblock %}</title>
<meta name="keywords" content="我的博客" />
<meta name="description" content="我的博客模板" />
<link href="/static/ckeditor/css/base.css" rel="stylesheet">
<link href="/static/ckeditor/css/index.css" rel="stylesheet">
<script src="/static/ckeditor/js/scrollReveal.js"></script>
</head>
<body>
 <div>
   <ul class="cbp_tmtimeline"> {% for i in blogs %} <li>
   <time class="cbp_tmtime"><span>{{ i.publish.month }}-{{ i.publish.day }}</span><span>{{ i.publish.year }}</span></time>
   <div class="cbp_tmicon"></div>
   <div class="cbp_tmlabel" data-scroll-reveal="enter right over 1s" >
    <h2>{{ i.title }}</h2>
    <p><span class="blogpic"><a href="{{ i.id }}"><img src="/static/{{ i.picture }}"></a></span>{{ i.description|safe }}</p>
     <a href="{{ i.id }}" target="_blank" class="readmore">閱讀全文&gt;&gt;</a>
       </div>
       </li> {% endfor %} </ul>
  </div>



# vim templates/app01/content.html
<dev>
<h2>{{ article.title }}</h2>
<p><span class="blogpic"><img src="/static/{{ article.picture }}"></span></p>
<span>{{ article.body|safe }}</span>
</dev>

修改路由:urls.py

from django.contrib import admin from django.urls import path,re_path,include from app01 import views urlpatterns = [ path('admin/', admin.site.urls), re_path('^$',views.index,name='index'), path(r'ckeditor/',include('ckeditor_uploader.urls')), re_path(r'(?P<article_id>\d)/$',views.center), #詳情頁獲取ID顯示頁面 ]

#重啓uwsgi和nginx服務....

#查看顯示頁:點擊詳情頁驗證!!!

 添加後臺過濾器:

#vim app01/admin.py

from django.contrib import admin from .models import blog_articles class BlogArticlesAdmin(admin.ModelAdmin): list_display = ("title","author","publish") list_filter = ("publish","author") search_fields = ('title','body') raw_id_fields = ("author",) date_hierarchy = "publish" admin.site.register(blog_articles,BlogArticlesAdmin)

 

(7)表單示例

經過template模版與MySQL實現簡單表單交互

在app目錄下的models文件中建立model類用於生成數據表:

#cat app01/models.py 
from django.db import models # Create your models here.

class userinfo(models.Model): name = models.CharField(max_length=32) password = models.CharField(max_length=32) age = models.IntegerField() salary = models.IntegerField()

設置setting.py文件,將app加入到INSTALLED_APPS中:

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01' ]

根據model類建立數據庫表:

#cmd進入django項目路徑下 #python manage.py migrate #建立表結構,非model類的其餘表,django所須要的
#python manage.py makemigrations app名 #作數據遷移的準備
如:python manage.py makemigrations app01 app01是項目中的app名字 #python manage.py migrate # 執行遷移,建立medel表結構

在templages下創建模版文件:

#cat templates/app01/home.html <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style> body{ background-image: url('/static/78556.jpg');
    }
    </style>
</head>
<body>
<form action="" method="post"> #提交數據給自身 <p><input type="text" name="username"/></p>
      <p><input type="text" name="password"/></p>
      <p><input type="text" name="age"/></p>
      <p><input type="text" name="salary"/></p>
      <p><input type="submit" value="提交"/></p>
</form>
<table border="1">
   <thead>
       <tr>
                <th>用戶名</th>
                <th>密碼</th>
                <th>年齡</th>
                <th>工資</th>
       </tr>
   </thead>
            <tbody> {% for item in data %} #循環獲取傳入字典數據 <tr>
                    <td>{{item.name}}</td>
                    <td>{{item.password}}</td>
                    <td>{{item.age}}</td>
                    <td>{{item.salary}}</td>
                </tr> {% endfor %} </tbody>
        </table>


<h1>this is test file</h1>
<script src="/static/jquery-3.3.1.js"></script>
</body>
</html>

在app下新建視圖函數,與數據庫交互:

#cat app01/views.py

from django.shortcuts import render from django.http import HttpResponse from app01 import models   #引入數據類模版 # Create your views here.

def home(request):   #建立home函數處理請求
  if request.method == "POST":   #判斷是否爲post提交
    #print(request.POST)
    models.userinfo.objects.create(     #提交表單的數據到數據庫
    name = request.POST['username'], password = request.POST['password'], age = request.POST['age'], salary = request.POST['salary'], ) data = models.userinfo.objects.all()   #獲取數據庫數據
  return render(request,'app01/home.html',{'data':data}) #渲染模版文件並傳遞數據庫表給模版

#此處是以post方式提交,須要修改Django項目setting設置中的MIDDLEWARE,將csrf安全機制註銷了:

MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

#創建路由:

#cat mysite/urls.py
from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path('home/',views.home), ]

從新啓動uWSGI:

#uwsgi --stop uwsgi.pid #uwsgi --ini uwsgi.ini

#瀏覽器訪問:http://192.168.146.139/home

#提交數據到數據庫後並返回給瀏覽器客戶端

虛擬環境搭建實例:

echo 'PS1="[\[\e[32;40m\]\u\[\e[0m\]@\[\e[32;40m\]\h\[\e[0m\]@\[\e[33;40m\]\A\[\e[0m\]\[\e[36;40m\]\W\[\e[0m\]\[\e[35;40m\]<\#>\[\e[0m\]]\\$"' ~/.bashrc . ~/.bashrc #配置epel源:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum clean all yum makecache #按照依賴包
yum install libffi-devel openssl openssl-devel zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel #安裝Python3.7
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz tar xvf Python-3.7.0.tar.xz mkdir -p /usr/local/python37 cd Python-3.7.0 ./configure --prefix=/usr/local/python37/ make && make install ln -s /usr/local/python37/bin/python3 /usr/local/bin/python3 ln -s /usr/local/python37/bin/pip3 /usr/local/bin/pip3 pip3 install --upgrade pip #配置國內python源
mkdir -p ~/.pip cat > ~/.pip/pip.conf << EFO [global] timeout=60 index-url=https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=https://pypi.tuna.tsinghua.edu.cn EFO #建立虛擬環境
python3 -m venv /mysite cd mysite/ git clone git://github.com/kennethreitz/autoenv.git echo 'source /mysite/autoenv/activate.sh' >> ~/.bashrc source ~/.bashrc echo "source /mysite/bin/activate" >/mysite/.env #在虛擬環境中安裝Django
pip install django django-admin.py startproject myblog cd myblog/

#測試Django
vim /mysite/myblog/myblog/settings.py ALLOWED_HOSTS = ["*"] python manage.py runserver 0.0.0.0:8000

#在虛擬環境中安裝Uwsgi
pip install uwsgi #測試uwsgi #建立測試文件
cat test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] uwsgi --http :9000 --wsgi-file test.py #使用uwsgi測試django
uwsgi --http :9000 --module myblog.wsgi #安裝nginx
yum install nginx systemctl enable nginx systemctl start nginx #nginx配置django和uwsgi反向代理:
location /static { alias /mysite/myblog; } location / { uwsgi_pass 127.0.0.1:9000; include /etc/nginx/uwsgi_params; uwsgi_param UWSGI_DIR /mysite/myblog; uwsgi_param UWSGI_MODULE myblog.wsgi; uwsgi_param UWSGI_ENV /mysite; } #contOS7建立系統服務
cat /etc/systemd/system/uwsgi.service [Unit] Description=uWSGI Emperor After=syslog.target [Service] ExecStart=/root/uwsgi/uwsgi --ini /etc/uwsgi/emperor.ini # Requires systemd version 211 or newer
RuntimeDirectory=uwsgi Restart=always KillSignal=SIGQUIT Type=notify StandardError=syslog NotifyAccess=all [Install] WantedBy=multi-user.target #uwsgi配置文件:
[uwsgi] socket = 127.0.0.1:9000 chdir = /root/mysite/myblog module = myblog.wsgi master = true processes = 2 threads = 2 max-requests = 2000 vacuum = true #home = /root/mysite
daemonize = /var/log/uwsgi/uwsgi.log stats = 127.0.0.1:9001 post-buffering = 65535 buffer-size = 65535 harakiri-verbose = true harakiri = 300 pidfile = /run/uwsgi.pid venv = /root/mysite/.venv #經常使用選項:
--http 在指定的地址上添加http端口 --http-socket  使用HTTP協議綁定到指定的UNIX/TCP套接字上 --wsgi-file 加載WSGI文件 --processes 指定進程數 --threads 指定每一個進程的線程數 -M --master 啓用主進程 --stats  指定狀態查詢綁定地址端口,如:127.0.0.1:9001
-s --socket  使用默認協議綁定到指定的UNIX/TCP套接字 --chdir:指定工程的絕對路徑,如Django的項目路徑 --module:指定web應用的API,如Django項目下的wsgi.py接口文件 --max-requests:最大請求數 --daemonize:指定uWSGI日誌文件路徑 --post-buffering:設置緩衝區 --buffer-size:設置緩衝區文件大小 --harakiri-verbose:設置超時true爲開啓 --harakiri:設置超時時間 --uid、--gid:設置用戶和組 --pidfile:指定啓動時的pid文件路徑 --venv  指定python虛擬環境
View Code

---------------------------------------------------------------------------------end

相關文章
相關標籤/搜索