meeb / django-distill

Minimal configuration static site generator for Django
MIT License
441 stars 35 forks source link

Change Detection? #39

Closed chrisspen closed 3 years ago

chrisspen commented 3 years ago

I think this may be a feature request, but does django-distill support generating static pages only for database records that have changed since the last generation, or does it re-generate all static pages every time the distill-local command is run?

meeb commented 3 years ago

Good question that's not in the docs anywhere! django-distill doesn't actually do either of those. What it does is just generate the pages that match the URLs which have parameters returned by the function pointed to in each URLs distill_func=.

For example, this will generate a URL for every hypothetical blog post in your database:

def get_all_blogposts():
    for post in Post.objects.all():
        yield {'blog_id': post_id, 'blog_title': post.title}

urlpatterns = (
    # e.g. /post/123-some-post-title.html using named parameters, yielded by get_all_blogposts
    distill_path('post/<int:blog_id>-<slug:blog_title>.html',
                 PostView.as_view(),
                 name='blog-post',
                 distill_func=get_all_blogposts),
)

If you want to just generate a subset of pages that's entirely up to your implementation. For example, lets say you add a changed field to your Post model of type BooleanField and you set changed=True each time you save a Post using a signal. You could then modify your distill_func to be something like:

def get_changed_blogposts():
    for post in Post.objects.filter(changed=True):
        yield {'blog_id': post_id, 'blog_title': post.title}
        post.changed = False
        post.save()

Obviously nested database operations are not performant, but in this context of one-off calls from a command line or CI process or something it's probably file.

django-distill, if using the distill-publish command to an S3 or GC bucket does only upload what's changed by comparing local and remote file hashes.

Feel free to ask for more details if this isn't quite what you want.

chrisspen commented 3 years ago

Thanks.