Skip to main content

Build html files from Django views.

Project description

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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

frozen-django-1.0.4.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

frozen_django-1.0.4-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file frozen-django-1.0.4.tar.gz.

File metadata

  • Download URL: frozen-django-1.0.4.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.2

File hashes

Hashes for frozen-django-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e76575f1838f8e037ccb5c09ab0cd7111bf1467a15f6c79fb52941f34a5a7db1
MD5 ba66cabf9bd2648fb7bea03a6c90af21
BLAKE2b-256 b7a2aa23879c09f2de616c86c29b310e2c54188178bfc4149609ed18f48e3e86

See more details on using hashes here.

File details

Details for the file frozen_django-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for frozen_django-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8b4acdf6ae9cca6aa3c3acade86230510d075f570721e47bf920c72874d912d8
MD5 b751f9a95e817c7c20291831a60e85ce
BLAKE2b-256 daa2bfa1641c3aaa655f7a8d635f6ee120c83f232fe8196e436f2156442ec8b0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page