Django的静态站点呈现程序和发布程序。
django-distill的Python项目详细描述
django蒸馏水
django district是一个最小配置的静态站点生成器和发布器
为Django。至少需要django 1.10或django 2.0。
django district
扩展现有的django站点,使其能够导出
全功能静态站点。它适用于像博客这样的网站
一个基本上是静态的前端,但是您仍然希望使用cms来管理
内容:
它直接插入到现有的django框架中,无需编写 自定义呈现程序或其他更详细的代码。你也可以充分利用现有的 动态站点,只为一小部分页面生成静态页面 而不是整个网站。
对于CDN上的静态文件,我们使用下面的"cache buster"库来允许 用于在推送更改时快速进行静态媒体更新:
https://github.com/mgrp/django-cachekiller
安装
从PIP安装:
$ pip install django-distill
在设置中的
已安装应用程序中添加
django_district
:
INSTALLED_APPS+=('django_distill',)
就这样。
限制
django district
生成静态页面,因此仅允许
get
支持返回http 200
状态代码的请求。
假设您使用的是uri参数,例如/blog/123 abc
,而不是
查询字符串参数,如/blog?post_id=123&title=abc
。查询字符串
由于明显的原因,参数对于静态页面生成没有意义。
另外,对于一次性静态页面,动态国际化将不起作用
因此,所有文件都是使用
settings.py
静态媒体文件(如图像和样式表)是从静态
在static\u root
中定义的媒体目录。这意味着你会想逃跑
/manage.py collectstatic
在运行/manage.py district local之前
如果您对静态媒体进行了更改。
django蒸馏器
按设计请求,但是您可以使用
--collectStatic
参数。
用法
假设您有一个现有的django项目,编辑aurl.py
以包含
提取路径
函数,它取代了django的标准路径
函数和
支持新的关键字参数提取函数
和提取文件
。这个
提取函数
参数应与函数或可调用类一起提供
返回iterable或none的。提取文件
参数完全
可选,并允许您重写否则将生成的url
从url regex的背面。这允许您将url重命名为
/example
到任何其他名称,如example.html
。从v0.8开始任何uri结束
斜线中的/
将自动修改为以/index.html
结尾。一个例子
理论博客应用程序的精简设置为:
# Replaces the standard django.conf.path, identical syntaxfromdjango_distillimportdistill_path# Views and models from a theoretical blogging appfromblog.viewsimportPostIndex,PostView,PostYearfromblog.modelsimportPostdefget_index():# The index URI path, '', contains no parameters, named or otherwise.# You can simply just return nothing here.returnNonedefget_all_blogposts():# This function needs to return an iterable of dictionaries. Dictionaries# are required as the URL this distill function is for has named parameters.# You can just export a small subset of values here if you wish to# limit what pages will be generated.forpostinPost.objects.all():yield{'blog_id':post_id,'blog_title':post.title}defget_years():# You can also just return an iterable containing static strings if the# URL only has one argument and you are using positional URL parameters:return(2014,2015)# This is really just shorthand for ((2014,), (2015,))urlpatterns=(# e.g. / the blog indexdistill_path('',PostIndex.as_view(),name='blog-index',distill_func=get_index,# / is not a valid file name! override it to index.htmldistill_file='index.html'),# e.g. /post/123-some-post-title using named parametersdistill_path('post/<int:blog_id>-<slug:blog_title>',PostView.as_view(),name='blog-post',distill_func=get_all_blogposts),# e.g. /posts-by-year/2015 using positional parametersdistill_path('posts-by-year/<int:year>',PostYear.as_view(),name='blog-year',distill_func=get_years),)
您的网站仍将与上述更改功能相同。内部的
提取函数
和提取文件
参数被删除,URL为
传递回django进行正常处理。这没有运行时性能
影响,因为这只在启动应用程序时发生一次。
您也可以使用提取路径
函数,它将替换默认的
django.url.re_path
函数。其用法与上述相同:
fromdjango_distillimportdistill_re_pathurlpatterns=(distill_re_path(r'some/regex'SomeOtherView.as_view(),name='url-other-view',distill_func=some_other_func),)
如果在1.x系列中使用较旧版本的django,则可以使用
提取url
函数代替django.conf.urls.url
或
django.url.url
函数。其用法与上述相同:
fromdjango_distillimportdistill_urlurlpatterns=(distill_url(r'some/regex'SomeView.as_view(),name='url-view',distill_func=some_func),)
注意django蒸馏
将镜像任何已安装的django版本
支持,因此在某个时刻提取URL
函数将停止工作
在将来,当django 2.x本身降低了django.conf.url.url的django.url.url
函数。您可以使用提取路径
更换。建议使用
提取路径
或提取路径
如果
您现在正在创建一个新站点。
提取本地
命令
一旦你包装了你想静态生成的url,你现在可以 生成一个功能完整的静态站点:
$ ./manage.py distill-local [optional /path/to/export/directory]
在引擎盖下,它只需迭代所有用district\uurl
注册的url,然后
使用django测试框架的一部分为它们生成页面
欺骗请求。一旦呈现了站点页面,则来自
静态根目录
被复制。在中替换具有相同名称的现有文件
目标目录和孤立文件将被删除。
提取本地
支持以下可选参数:
--collectStatic
:在您的站点上自动运行
渲染,这只是一个快捷方式,可以节省您键入额外命令的时间。
--quiet
:禁用除询问确认问题以外的所有输出。
--force
:对所有确认问题假定"是"。
注意如果任何视图包含python错误,则呈现将失败 然后堆栈跟踪将被打印到终端和呈现命令 将退出,状态代码为1。
提取发布
命令
$ ./manage.py distill-publish [optional destination here]
如果您至少配置了一次发布目标(见下文),则可以
使用提取发布
命令将站点发布到远程位置。
这将执行完全同步,删除所有
在生成的静态站点中存在更长时间并上载任何新的或更改的
文件夹。当
网站发布后删除的发布。每个文件
通过
公共URL
提取发布
支持以下可选参数:
--collectStatic
:在您的站点上自动运行
渲染,这只是一个快捷方式,可以节省您键入额外命令的时间。
--quiet
:禁用除询问确认问题以外的所有输出。
--force
:对所有确认问题假定"是"。
注意这意味着如果使用--force
和--quiet
输出
目录将删除不属于站点导出的所有文件,而不删除任何
确认。
注意如果任何视图包含python错误,则呈现将失败 然后堆栈跟踪将被打印到终端和呈现命令 将退出,状态代码为1。
提取测试发布
命令
$ ./manage.py distill-test-publish [optional destination here]
这将连接到您的发布目标,对其进行身份验证,上载
随机命名的文件,确认它存在于public\u url
上,然后将其删除
再一次。使用此项检查您的发布设置是否正确。
提取测试发布
没有参数。
可选配置设置
您可以设置以下可选的设置.py
变量:
提取目录:字符串,要导出到的默认目录:
DISTILL_DIR='/path/to/export/directory'
District_Publish:字典与django的设置类似。数据库支持
默认值
:
$ pip install django-distill0
发布目标
您可以通过
就像你能做的那样使用mysql、sqlite、postgresql等
Django通过更改后端数据库引擎。当前支持的引擎
通过django district
是:
django_district.backends.rackspace_文件:发布到rackspace云文件
容器。需要python库pyrax
($pip install pyrax
)。这个
容器必须已存在(使用Rackspace云控制面板)。选项:
$ pip install django-distill1
django_district.backends.amazon_s3:发布到amazon s3存储桶。要求
python库boto
($pip install boto
)。水桶必须已经
存在(使用AWS控制面板)。选项:
$ pip install django-distill2
django_district.backends.google_storage:发布到google云存储
桶。需要python库google api python client
和
谷歌云存储
($pip安装google api python客户端google云存储
)。桶
必须已经存在并设置为承载公共静态网站(使用
谷歌云控制面板)。选项:
$ pip install django-distill3
测试
有一个最小的测试套件,你可以通过关闭这个存储库来运行它,
在requirements.txt中安装所需的依赖项,然后执行:
$ pip install django-distill4
贡献
所有格式正确且合理的请求、问题和注释 欢迎,