Django Tips
傳統的 HTTP/1.1 在每個 TCP 連線中只允許向 server 發送單一個請求,但當網頁載入時,往往會需要向同一個伺服器發送多個請求(例如、圖檔、CSS、靜態檔、JS 等),因此為了要避開這樣的限制、加快載入的速度,瀏覽器會實作多個平行的(parallel) TPC 連線(每個瀏覽器實作不同,因此數量的上限也不同),以處理同時向伺服器發出的多個請求。
在 HTTP/2 中則可在同一個 TCP 連線中進行多個請求和回應,並且可以由 server 主動推送資源給 client,而並非一定要透過 client 主動請求;此外支援 HTTP Header 的壓縮,減少資料傳數量;HTTP/2 也是使用 binary 的方式在傳輸資料。
Instagram 擁有 30億註冊用戶,已超過 32bit int 限額,都沒問題。客製化工作包括: 讓 Models 支援 Sharding,手動關閉 Garbage Collection 來提昇記憶體管理效率,在各處資料中心部署整套系統。效能改善策略包括: 開發工具來調整效能調校,使用 C/C++ 重寫部份組件,使用 Cython, asyncio。昇級到 Python3 的策略,是讓 code base 同時相容 2 和 3 拒絕引入不相容 3 的函式庫,除去不再使用的函式庫,替換不相容的函式庫。昇級過程要處理 Unicode 問題,利用 ensure_str(), ensure_binary(), ensure_text() 來檢查,要處理 pickle 的差異,利用不同 namespace 來區隔,要處理 Iterator 內容只被造訪一次的問題,轉換成 List 可改善,要處理 Dictionary 順序問題,利用 sort_keys=True 來改善。
basic folder structure 共用 Model 的變通方式 AutoField use_for_related_fields 的運作原理 REST API DataTables PostgreSQL JSON Generation
Youtube Example REST with React
Visual Web Designer HTML5 Boilerplate
Python From Scratch - Create a Dynamic Website
API + Dashboard: Throughput (RPM), Cache
Convert Django QuerySet to Pandas DataFrame in 跟 contains 無法併存
args = functools.reduce(operator.or_,(Q(name__icontains=i) for i in pattern)) queryset.filter(args)
django-channels 會處理 WebSockets 和 HTTP2 在內的內容,建立背景運算之類的功能,但要注意 asgi 和 wsgi 分開服務,以及資料上傳量。
channels example tutorial channels2 利用 console 程式 websocket 來呼叫印表機 http://radio-idea.blogspot.com/2018/12/blog-post.html
Wagtail
Wagtail: How to Make Your Own Content Type Model #1 PostGIS Real Estate: OpenIMOB
$ sudo apt-get install libjpeg-dev zlib1g-dev python-pip python-virtualenv python-pil # Python3: python3-pip python3-virtualenv python3-pil
$ virtualenv py2env
# Python3: virtualenv -p python3 py3env
$ cd pyNenv $ source bin/activate $ pip install wagtail ... Successfully installed Django-1.10.2 Pillow-3.4.1 Unidecode-0.4.19 Willow-0.3.1 beautifulsoup4-4.5.1 django-modelcluster-2.0 django-taggit-0.18.3 django-treebeard-4.0.1 djangorestframework-3.4.7 html5lib-0.999999 pytz-2016.7 six-1.10.0 wagtail-1.6.3 $ wagtail start mysite Creating a Wagtail project called mysite Success! mysite has been created $ cd mysite $ pip install -r requirements.txt $ python manage.py migrate $ python manage.py createsuperuser $ python manage.py runserver $ vi home/models.py $ python manage.py makemigrations $ python manage.py migrate $ vi home/templates/home/home_page.html
啟動 ElasticSearch 服務後,就可以安裝 Python Client 並設定整合:
$ pip install elasticsearch $ vi mysite/settings/base.py WAGTAILSEARCH_BACKENDS = { 'default': { 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch', 'URLS': ['http://localhost:9200'], 'INDEX': 'wagtail', 'TIMEOUT': 5, } } $ python manage.py update_index Updating backend: default default: Rebuilding index wagtail default: home.HomePage . default: wagtaildocs.Document default: wagtailimages.Image default: wagtailcore.Page . default: indexed 2 objects
ecommerce: snipcart
Deploying Wagtail to Heroku: SECRET_KEY
./manage.py dumpdata
針對「然而我覺得 forms.form 不直接跟databse互動,很抽象,輸入的表單不就存到DB嗎?不然submit後要跑去哪?」 其實不見得只有存到DB這種可能,表單也有可能只是作為搜尋條件來用(比如google進階搜尋那樣的表單),或者是後面會再接一個API通到其他服務上也有可能(比如結果存到google spreadsheet上),因此在這些不直接存到DB的情況,是不一定需要用到ModelForm,因為表單上的欄位不見得會與Model的欄位直接相關。 然而如果你要表單的一些欄位從Model來然後擴增一些欄位的話,也是可以從ModelForm做到的(畢竟他就是Form的subclass,Form能做到的基本他都能做到,就多了與model連結的功能,詳細可以讀一下ModelForm的文件 https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/#modelform ),但那些擴增欄位的儲存邏輯就要自己實作。
Arches
專為文化資產維護設計的系統,內含專業分類與流程模組。透過 Roadmap 了解主要特色。