Python WSGI 及其應用

tags: python, wsgi, gunicorn, uwsgi

Python WSGI及其應用

WSGI是什麼?

  • 全名Python Web Server Gateway Interface
  • 基於CGI
  • Python用來描述了伺服器和請求處理程式之間傳輸資料的一種標準(協議)

PEP3333

基本原理跟目標

server、app、framework共用的界面

…proposes a simple and universal interface between web servers and web applications or frameworks: the Python Web Server Gateway Interface (WSGI).

不再重造輪子

…the goal of WSGI is to facilitate easy interconnection of existing servers and applications or frameworks, not to create a new web framework

只會支援python既有的release

…precludes WSGI from requiring anything that is not already available in deployed versions of Python

未來會增加部署的標準

…current version of WSGI does not prescribe any particular mechanism for “deploying” an application for use with a web server or server gateway. At the present time, this is necessarily implementation-defined by the server or gateway

Read more “Python WSGI 及其應用” 發表留言

初始Django專案並客製化設定檔 – Django Setup With Split Settings

Django Setup 專案初始化

假設我們想要建立一個django專案(1.9版本,linux為例),命名為mysite,並且不想要將特定的設定上傳,可以如何做:

clone你的倉庫(myrepo)至本地

建立虛擬環境

可以透過指令或IDE,我習慣會將環境命名為venv,然後放在專案目錄下

安裝django並開始一個專案

初始化django專案

別忘了把你安裝的套件加到mysite/requirements.txt

這時候你的tree應該長這樣

Read more “初始Django專案並客製化設定檔 – Django Setup With Split Settings”

發表留言

Django: Create an instance of Manager with a copy of a custom QuerySet

Sometimes we want to query by custom filter and different orders

And in this case will raise AttributeError because Queryset object can not call Manager method

Solution: as_manager()

More Django settings from document:

發表留言

Django Unit Test with Fixtures

Sometimes we need to write test with initial data

For this setting as example, we can load initial data by

And we need to do setup in test cases

發表留言

Django: Query from abstract model class

Abstract classes such as AbstractProduct which inherit from third party package model_utils.InheritanceManage, will not work with abstract attribute specified true:

For this reason, its subclass like ProductA will cause create issue when load fixtures data by running this command:

Which will not raise errors also not writing into database, see the same issue on stackoverflow

The solution here is provide post_save signal and execute save(), because loaddata command will not call pre_save() and save()

發表留言

Django: rest_framework

ModelSerializer

data

retrieve lookup

Override APIView

permission

HyperlinkedIdentityField

MethodField

    發表留言