Django構建靜態網頁站點
If you’ve ever poked around on this blog, you may have noticed the colophon which mentions very briefly:
Unlike most Django sites, this is compiled into static HTML pages bydjango-medusa, a Django app I'm currently building.
This little tool has been open-source since I deployed to the new version of this website, maybe nine months ago, but I hadn’t really done anything with it or mentioned it much anywhere. It powers the several hundred pages on this site and turns them into static HTML — which is then hosted in S3. (Details below.)
(The only other time I’ve mentioned this project publicly was in response to django-bakery, a tool that the L.A. Times Data Desk uses to process some data projects into static pages. Clearly, this is an interesting idea to some people.)
tl;dr for Django pros: Test out the tutorial “hello world” and see the README. Come back if you want the more detailed narrative breakdown of the app (and how the app powers this blog).
The app basically auto-finds “renderer” definitions for your apps and then provides a Django management command that builds the static rendition of the website (with the output directed based on some settings).
Renderers live in renderer.py files that are auto-discovered — like models.py andtests.py, it’s auto-discovered as long as the app is listed in INSTALLED_APPS. This basically defines a class that defines a get_paths instance method that returns a list/tuple of absolute URLs, representing all the URLs that should be converted to static files. Renderers are set up like this so that, on an app-by-app basis (or even varying within an app), you can dynamically generate all the possible URLs that exist in your site.
Here’s a couple renderer definitions that actually power part of this site.
The specific URL names and model bits aren’t important: basically, you’ll notice that the example BlogHomeRenderer in my example generates the entire URL structure for/blog/* by querying for all live blog posts and then using Django’s URL reversing methods to figure out all the paths that could possibly be built. (That file in particular uses sets instead of lists/tuples, so that it can just blindly generate all the URLs and have duplicates ignored. It casts the set to a list upon returning.)
The process that actually generates the output simply uses (or abuses) Django’s internal testclient to request each URL and store the resulting data (and mimetype/other metadata, if using the right backend — I’ll touch on this more, below).
I believe that this paradigm provides the most flexibility regarding giving each app the ability to define it’s own outputs and it keeps app-and-view-building as Django-like as possible (i.e. you are still building within the urlconf and view system).
It seems ghetto at first to rely on those internal HTTP testclient mechanisms, but I haven’t yet encountered any issues — the rendering command can even (optionally) parallelize the testclient crawl to achieve faster rendering.
Unlike most Django sites, this is compiled into static HTML pages bydjango-medusa, a Django app I'm currently building.
This little tool has been open-source since I deployed to the new version of this website, maybe nine months ago, but I hadn’t really done anything with it or mentioned it much anywhere. It powers the several hundred pages on this site and turns them into static HTML — which is then hosted in S3. (Details below.)
(The only other time I’ve mentioned this project publicly was in response to django-bakery, a tool that the L.A. Times Data Desk uses to process some data projects into static pages. Clearly, this is an interesting idea to some people.)
tl;dr for Django pros: Test out the tutorial “hello world” and see the README. Come back if you want the more detailed narrative breakdown of the app (and how the app powers this blog).
The app basically auto-finds “renderer” definitions for your apps and then provides a Django management command that builds the static rendition of the website (with the output directed based on some settings).
Renderers live in renderer.py files that are auto-discovered — like models.py andtests.py, it’s auto-discovered as long as the app is listed in INSTALLED_APPS. This basically defines a class that defines a get_paths instance method that returns a list/tuple of absolute URLs, representing all the URLs that should be converted to static files. Renderers are set up like this so that, on an app-by-app basis (or even varying within an app), you can dynamically generate all the possible URLs that exist in your site.
Here’s a couple renderer definitions that actually power part of this site.
The specific URL names and model bits aren’t important: basically, you’ll notice that the example BlogHomeRenderer in my example generates the entire URL structure for/blog/* by querying for all live blog posts and then using Django’s URL reversing methods to figure out all the paths that could possibly be built. (That file in particular uses sets instead of lists/tuples, so that it can just blindly generate all the URLs and have duplicates ignored. It casts the set to a list upon returning.)
The process that actually generates the output simply uses (or abuses) Django’s internal testclient to request each URL and store the resulting data (and mimetype/other metadata, if using the right backend — I’ll touch on this more, below).
I believe that this paradigm provides the most flexibility regarding giving each app the ability to define it’s own outputs and it keeps app-and-view-building as Django-like as possible (i.e. you are still building within the urlconf and view system).
It seems ghetto at first to rely on those internal HTTP testclient mechanisms, but I haven’t yet encountered any issues — the rendering command can even (optionally) parallelize the testclient crawl to achieve faster rendering.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734279/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Django+nginx+靜態網站DjangoNginx網站
- 網站建設有必要做偽靜態嗎?網站
- 易優cms網站SEO模組URL配置偽靜態靜態頁面設定網站
- 深入瞭解網站中的靜態網頁和動態網頁的相關知識網站網頁
- 前端靜態站點部署方案前端
- 利用hugo生成靜態站點Go
- 靜態網頁與動態網頁的區別網頁
- 靜態網頁和動態網頁的區別網頁
- 網站靜態化思想網站
- 網站偽靜態配置網站
- 網站偽靜態和純靜態區別網站
- 深入學習和理解Django模板層:構建動態頁面Django
- 利用Django徒手寫個靜態頁面生成工具Django
- echarts map靜態套用網頁Echarts網頁
- tinysearch/tinysearch: 使用Rus和Wasm構建的靜態網站的微型全文搜尋引擎ASM網站
- 日記13(靜態網站)網站
- 網站提速-偽靜態(3)網站
- 談談網站靜態化網站
- Django靜態URL配置Django
- 將現有的靜態 Web 站點轉變為動態 Web 站點Web
- HTML+CSS編寫靜態網站-31 新增About頁面HTMLCSS網站
- 前後端,靜態網站和動態網站, 的理解後端網站
- 【靜態化平臺】☞(一)網頁靜態化服務網頁
- Gridsome 生成靜態站點基礎
- 用HTML+CSS編寫一個計科院網站首頁的靜態網頁HTMLCSS網站網頁
- C#靜態建構函式及靜態變數學習C#函式變數
- 一個簡單靜態網頁網頁
- 【靜態頁面架構】CSS之列表架構CSS
- 【靜態頁面架構】CSS之表格架構CSS
- HTML+CSS編寫靜態網站-33 建立Contact頁面HTMLCSS網站
- HTML+CSS編寫靜態網站-39 調整About頁面HTMLCSS網站
- Rust 的靜態網站生成器「GitHub 熱點速覽」Rust網站Github
- django 網站實現簡單分頁Django網站
- [譯] 用 Workers 讓靜態網站動態化網站
- 大型網站架構改進歷程(9):網站靜態化處理–總述(1)網站架構
- 靜態網頁簡易生成方法網頁
- Serverless實踐-靜態網站託管Server網站
- docker容器中佈置靜態網站Docker網站