Skip to main content

FROZEN DJANGO

Export webpages created by your Django views into html files.

It is dumb, it won’t deploy the files to AWS for instance, and won’t collect assets from static files or media files.

The assumption is that the build process will be triggered by Django signals, for example when a BlogPost was created, and will only rebuild the related html (or json) files.

Inspired by:

Requirements

  • add frozen_django to your INSTALLED_APPS

  • will only process urls with file extensions (.html, .json, .js, .xml, etc.)

  • Django Views with pagination must have Link HTTP header or html tag <link rel="next" /> in their content

Settings

  • FROZEN_ROOT (should be filled, can be dict for multisite, keys are items in ALLOWED_HOSTS)

  • FROZEN_MIDDLEWARE (optional)

API

  • Django Command freeze_view

  • frozen_django.tasks_celery.freeze_view

  • frozen_django.tasks_uwsgi.freeze_view

Examples

Here is an example of all pages rebuild:

File blog/settings.py

from django.utils.translation import gettext_lazy as _

ALLOWED_HOSTS = ['www.blog.id', 'www.planet.id']

INSTALLED_APPS = [
    'frozen_django',
]

MIDDLEWARE = [
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
]

FROZEN_MIDDLEWARE = [
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
]

USE_I18N = True
LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', _("English")),
    ('id', _("Bahasa Indonesia")),
)

FROZEN_ROOT = {
    'www.blog.id': ROOT_DIR/'public'/'blog',
    'www.planet.id': ROOT_DIR/'public'/'planet',
}

File blog/urls.py:

from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _
from website import views

urlpatterns = i18n_patterns(
    path(_('posts/'), include('blog_post.urls', namespace='blogpost')),
    path('index.<str:format>', views.Home.as_view(), name='home'),
    path('index/pages/<int:page>.<str:format>', views.Home.as_view(),
        name='homepages'),
    prefix_default_language=True,
)

File blog_post/urls.py:

from . import views

app_name = 'blog_post'

urlpatterns = [
    path('<str:slug>.<str:format>', views.Display.as_view(), name='display'),
]

File blog_post/signals.py

from website.tasks import hosts_freeze_view

def post_updated(sender, instance, **kwargs):
    hosts_freeze_view('website.views.Home', format='html')
    hosts_freeze_view('blog_post.views.Display', slug=instance.slug,
            format='html')

File blog_post/apps.py

from django.db.models.signals import post_save

class BlogPostConfig(AppConfig):
    name = 'blog_post'

    def ready(self):
        from . import models
        from .signals import post_updated

        post_save.connect(post_updated, sender=models.BlogPost)

File website/views.py

from django.views.generic import ListView
#-
from blog_post.models import Post

class Home(ListView):
    template_name = 'website/home.html'
    paginate_by = 2

    def get_queryset(self):
        return Post.objects.all()

File website/tasks.py

from django.conf import settings
from frozen_django.main import generate_static_view
from uwsgi_tasks import task, TaskExecutor

@task(executor=TaskExecutor.SPOOLER)
def freeze_view(view_name, base_url, **kwargs):
    generate_static_view(view_name, frozen_host=base_url, **kwargs)


def hosts_freeze_view(view_name, **kwargs):
    for host in settings.ALLOWED_HOSTS:
        freeze_view(view_name, base_url=host, **kwargs)

File website/templates/website/home.html

<html>
  <head>
    {% if page_obj.has_next %}
      <link rel="next" href="{% url 'homepages' page=page_obj.next_page_number format='html' %}">
    {% endif %}
  </head>
</html>

Release files for frozen-django 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for frozen-django 1.0.4
File Size Uploaded
frozen-django-1.0.4.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for frozen-django 1.0.4
File Interpreter ABI Platform
frozen_django-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / frozen-django-1.0.4.tar.gz

Download URL frozen-django-1.0.4.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e76575f1838f8e037ccb5c09ab0cd7111bf1467a15f6c79fb52941f34a5a7db1
BLAKE2b-256 checksum
How to use checksums
b7a2aa23879c09f2de616c86c29b310e2c54188178bfc4149609ed18f48e3e86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.2

Release files / frozen_django-1.0.4-py3-none-any.whl

Download URL frozen_django-1.0.4-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b4acdf6ae9cca6aa3c3acade86230510d075f570721e47bf920c72874d912d8
BLAKE2b-256 checksum
How to use checksums
daa2bfa1641c3aaa655f7a8d635f6ee120c83f232fe8196e436f2156442ec8b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.2

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page