django staticflatpages
1.0.0
它就像平面頁面,但有模板
這就像 Django 的內建contrib.flatpages應用程序,但沒有資料庫。它只是從檔案系統提供的靜態 html 文件。
我已經使用flatpages應用程式很長一段時間了,但在某個時候,我開始將我的 Flatpages 內容(html 片段)與專案的其餘部分一起保存在 git 儲存庫中。每當我對平面頁面進行更改時,我都會在本地編輯文件,提交更改,然後將新內容複製並貼上到相關平面頁面中。
為什麼不直接從我的模板目錄中提供這些服務呢?
這就是staticflatpages作用。
使用 pip 安裝最新版本:
pip install django-staticflatpages
該應用程式的最新版本是針對 Django 2.1+ 和 Python 3.6+。下載舊版 Django 的先前版本。
您可以使用python manage.py test staticflatpages運行測試套件,並在遇到任何問題時在 Github 上開啟問題。
staticflatpages加入您的INSTALLED_APPS 。staticflatpages.middleware.StaticFlatpageFallbackMiddleware加入您的MIDDLEWARE設定。staticflatpages範本目錄。這應該是TEMPLATES設定中範本之一的子目錄。您在此處包含的任何模板( base.html除外)都將作為靜態頁面提供。例如,假設您的專案級範本目錄名為“templates”,則:
/about/將呈現templates/staticflatpages/about.html/about/team/將呈現templates/staticflatpages/about/team.htmltemplates/staticflatpages/index.html ),並且沒有其他 url 映射到/ ,它將用作您的索引。 該應用程式還支援靜態平面頁面的網站地圖。要啟用這些功能,您需要在INSTALLED_APPS設定中列出django.contrib.sitemaps 。然後,設定網站地圖(例如在您的根 URLconf 中):
from staticflatpages.sitemaps import StaticFlatpageSitemap
sitemaps = {
'staticflatpages': StaticFlatpageSitemap,
}
不要忘記像平常一樣包含您的網站地圖網址,例如:
path(
'sitemap-<section>.xml',
sitemaps_views.sitemap,
{'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'
),
path(
'sitemap.xml',
sitemaps_views.index,
{'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'
),
注意: sitemaps框架還需要sites框架,因此您需要安裝該框架,並且還需要定義SITE_ID 。
如果您使用網站地圖功能,您可能還需要包含以下設定:
STATICFLATPAGES_CHANGEFREQ :對應於Sitemap.changefreq屬性(預設為never )。STATICFLATPAGES_PRIORITY :對應於Sitemap.priority屬性(預設為 0.5)。 這個應用程式可以與 django-dirtyedit 一起使用,它允許您從管理員編輯檔案(如果您願意的話)。
該程式碼是根據 MIT 授權條款分發的。請參閱LICENSE文件。