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網站
- 構建靜態頁面 之 [ 列表 ]
- 構建靜態頁面 之 [ 表格 ]
- 構建靜態頁面 之 [ 表單 ]
- 網站建設有必要做偽靜態嗎?網站
- 靜態網站如何修改,輕鬆更新網頁內容網站網頁
- php網站首頁動態地址修改,如何將PHP網站首頁的動態地址改為靜態地址PHP網站
- 靜態網頁與動態網頁的區別網頁
- 易優cms網站SEO模組URL配置偽靜態靜態頁面設定網站
- 前端靜態站點部署方案前端
- 網站偽靜態配置網站
- 網站靜態化思想網站
- 深入學習和理解Django模板層:構建動態頁面Django
- 利用Django徒手寫個靜態頁面生成工具Django
- echarts map靜態套用網頁Echarts網頁
- Gridsome 生成靜態站點基礎
- tinysearch/tinysearch: 使用Rus和Wasm構建的靜態網站的微型全文搜尋引擎ASM網站
- 前後端,靜態網站和動態網站, 的理解後端網站
- 用HTML+CSS編寫一個計科院網站首頁的靜態網頁HTMLCSS網站網頁
- 一個簡單靜態網頁網頁
- 【靜態頁面架構】CSS之列表架構CSS
- 【靜態頁面架構】CSS之表格架構CSS
- django 網站實現簡單分頁Django網站
- Rust 的靜態網站生成器「GitHub 熱點速覽」Rust網站Github
- [譯] 用 Workers 讓靜態網站動態化網站
- 使用 Hexo 為自己在 Github 上建一個靜態 Blog部落格 站點HexoGithub
- 網頁靜態化之freemaker的使用網頁
- 介紹Cloudflare頁面:構建JAMstack網站的最佳方法Cloud網站
- PbootCMS網站IIS偽靜態規則boot網站
- PbootCMS網站apache偽靜態規則boot網站Apache
- PbootCMS網站nginx偽靜態規則boot網站Nginx
- Serverless實踐-靜態網站託管Server網站
- docker容器中佈置靜態網站Docker網站
- 專業建站網站 網站建設 網站開發 官網開發 網頁設計 網頁網站網頁
- 基於React的SSG靜態站點渲染方案React
- 【靜態頁面架構】CSS之盒子模型架構CSS模型
- 【靜態頁面架構】CSS之選擇器架構CSS
- 靜態網站託管服務平臺網站
- 30分鐘搭建你的靜態網站網站