Skip to main content

Subjugate

Write your Django templates with Python!

  • All built-in Django loaders work.
  • All built-in filters work.
  • Caching and autoreloading works.

The missing companion library for Django to use dominate to write templates.

Installation

pip install subjugate

# Subjugate doesn't actually depend on Domainate but it's highly highly recommended.
pip install domainate
INSTALLED_APPS = [
    ...,
    "subjugate",
    ...
]

TEMPLATES = [
  ...,
  {
    "BACKEND": "subjugate.template.backends.subjugate.SubjugateTemplates",
    "DIRS": [],
    "APP_DIRS": True,
    "OPTIONS": {"debug": False},
  }
]

Usage

Assuming you're using APP_DIRS create a file yourapp/subjugate/yourapp/page.py.

from subjugate import SubjugateTemplate
from dominate import tags as t

class PageTemplate(SubjugateTemplate):
  def render(self, title="Page", description="Cool stuff here", **kwargs):
        document = dominate.document(title=title)
        abs_url = self.request.build_absolute_uri()

        with document.head:
            t.meta(name="charset", content="UTF-8")
            t.meta(name="viewport", content="width=device-width, initial-scale=1")
            t.title(title)
            t.script(
                """
                document.documentElement.classList.remove('no-js');\
                document.documentElement.classList.add('js');
                """,
                type="module",
            )
            t.base(href=f"{abs_url}")
            t.link(rel="canonical", href=f"{abs_url}")
            t.link(rel="author", href=f"{self.static('humans.txt')}")
            t.link(rel="license", href=f"{self.url('copyright')}")
            t.meta(name="description", cotent=description)
            t.meta(property="og:title", content=title)
            t.meta(property="og:locale", content="en_US")
            t.meta(property="og:type", content="website")
            t.meta(property="og:url", content=f"{abs_url}")

        with document:
          t.p("Hello World!")

        return document

Then in urls.py

urlpatterns = [
  ...,
  path("page/", TemplateView.as_view(template_name="yourapp/page.py")),
]

Template Reuse

class BaseTemplate(SubjugateTemplate):
  def render(self, title="Blah", **kwargs):
    document = dominate.document(title=title)

    with document.head:
      t.title(title)

    return document

class DerivedTemplate(SubjugateTemplate):
  def render(self, **kwargs):
    base = self.extend("yourapp/base.py", title="Title Works")

    with base:
      t.p("Hello from the derived template!")

    return base

Other Features

class TemplateVars(SubjugateTemplate):
  def render(self, **kwargs):
    base = self.extend("yourapp/base.py", title="Title Works")

    with base:
      # Context Vars
      t.p(self.vars.message)

      # Page URLs
      t.p(self.url('yourapp/copyright.py'))

      # Static URLs
      t.p(self.static('yourapp/humans.txt'))

      # CSRF Tokens
      t.p(self.csrf_token())

      # Filters
      t.p(self.filter("upper", "yes"))

      # Lorem Ipsum
      t.p(self.lorem_words(count=15))
      t.p(self.lorem_paragraphs(count=3))

    return base

But I don't want to use Domainate

Cool cool, like I said this library doesn't actually require Domainate. Just make sure that the thing your render function returns has a __str__ method that actually outputs the renderd HTML.

Authors

Release files for subjugate 1.0.0

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

Source distribution (sdist)

Source distribution for subjugate 1.0.0
File Size Uploaded
subjugate-1.0.0.tar.gz 9.9 kB Details

Release files / subjugate-1.0.0.tar.gz

Download URL subjugate-1.0.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d767f2e58c4e74c1b9a9ffe002dcd9f68a527722db6442c0b08ce542945f6e5b
BLAKE2b-256 checksum
How to use checksums
0e4e09801cf3b14a11d5ce6e4990d79ec1dbcab799d17cb983c2d3cd3860c052
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

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