• Get Review Board
  • What's New
  • Products
  • Review Board Code review, image review, and document review
  • Documentation
  • Release Notes
  • Power Pack Enterprise integrations, reports, and enhanced document review
  • Try for 60 Days
  • Purchase
  • RBCommons Review Board as a Service, hosted by us
  • Pricing
  • RBTools Command line tools and Python API for Review Board
  • Documentation
  • Release Notes
  • Review Bot Automated code review, connecting tools you already use
  • Documentation
  • Release Notes
  • RB Gateway Manage Git and Mercurial repositories in your network
  • Documentation
  • Release Notes
  • Learn and Explore
  • What is Code Review?
  • Documentation
  • Frequently Asked Questions
  • Support Options
  • Third-Party Integrations
  • Demo
  • Review Board RBTools Power Pack Review Bot Djblets RB Gateway
    1. Djblets 6.x
    2. Version 6.x
    3. Version 5.x
    4. Version 4.x
    5. Version 3.x
    6. Version 2.x
    7. Version 2.0
    8. Version 1.0
    9. Version 0.9
    10. Djblets Documentation
    11. Guides
    12. Dynamic Page Injections
  • Home
  • Guides
  • Avatar Services Guides
  • Writing Avatar Services
  • Caching Guides
  • Caching Data
  • Working with Cache Keys
  • Cache Backends
  • Extension Guides
  • Writing Extensions
  • Testing Extensions
  • Feature Checks Guides
  • Introduction to Feature Checks
  • Writing Features
  • Writing Feature Checkers
  • Testing with Feature Checks
  • Integration Guides
  • Supporting Integrations
  • Writing Integrations
  • Dynamic Page Injections
  • Privacy Compliance Guides
  • Getting and Checking Consent
  • Working with Personally Identifiable Information
  • Service Integrations
  • Protection Guides
  • Rate Limiting Operations
  • reCAPTCHA Guides
  • Using reCAPTCHA
  • Registries Guides
  • Writing Registries
  • Web API Guides
  • Writing Web API Resources
  • Adding OAuth2 Support
  • Module and Class References
  • djblets
  • djblets.deprecation
  • djblets.auth.forms
  • djblets.auth.ratelimit
  • djblets.auth.signals
  • djblets.auth.util
  • djblets.auth.views
  • djblets.avatars.errors
  • djblets.avatars.forms
  • djblets.avatars.registry
  • djblets.avatars.services
  • djblets.avatars.services.base
  • djblets.avatars.services.fallback
  • djblets.avatars.services.file_upload
  • djblets.avatars.services.gravatar
  • djblets.avatars.services.url
  • djblets.avatars.settings
  • djblets.cache
  • djblets.cache.backend
  • djblets.cache.backend_compat
  • djblets.cache.context_processors
  • djblets.cache.errors
  • djblets.cache.forwarding_backend
  • djblets.cache.serials
  • djblets.cache.synchronizer
  • djblets.conditions
  • djblets.conditions.choices
  • djblets.conditions.conditions
  • djblets.conditions.errors
  • djblets.conditions.operators
  • djblets.conditions.values
  • djblets.configforms.forms
  • djblets.configforms.mixins
  • djblets.configforms.pages
  • djblets.configforms.registry
  • djblets.configforms.views
  • djblets.datagrid
  • djblets.datagrid.grids
  • djblets.db.backends.mysql.base
  • djblets.db.fields
  • djblets.db.fields.base64_field
  • djblets.db.fields.comma_separated_values_field
  • djblets.db.fields.counter_field
  • djblets.db.fields.json_field
  • djblets.db.fields.modification_timestamp_field
  • djblets.db.fields.relation_counter_field
  • djblets.db.managers
  • djblets.db.query
  • djblets.db.query_catcher
  • djblets.db.query_comparator
  • djblets.db.validators
  • djblets.pagestate
  • djblets.pagestate.injectors
  • djblets.pagestate.middleware
  • djblets.pagestate.state
  • djblets.pagestate.templatetags.djblets_pagestate
  • djblets.extensions
  • djblets.extensions.admin
  • djblets.extensions.errors
  • djblets.extensions.extension
  • djblets.extensions.forms
  • djblets.extensions.hooks
  • djblets.extensions.loaders
  • djblets.extensions.manager
  • djblets.extensions.middleware
  • djblets.extensions.models
  • djblets.extensions.packaging
  • djblets.extensions.resources
  • djblets.extensions.settings
  • djblets.extensions.signals
  • djblets.extensions.staticfiles
  • djblets.extensions.testing
  • djblets.extensions.testing.testcases
  • djblets.extensions.urls
  • djblets.extensions.views
  • djblets.extensions.templatetags.djblets_extensions
  • djblets.features
  • djblets.features.checkers
  • djblets.features.decorators
  • djblets.features.errors
  • djblets.features.feature
  • djblets.features.level
  • djblets.features.registry
  • djblets.features.testing
  • djblets.features.templatetags.features
  • djblets.forms.fields
  • djblets.forms.fieldsets
  • djblets.forms.forms
  • djblets.forms.forms.key_value_form
  • djblets.forms.widgets
  • djblets.gravatars
  • djblets.gravatars.templatetags.gravatars
  • djblets.http.middleware
  • djblets.http.requests
  • djblets.http.responses
  • djblets.integrations.errors
  • djblets.integrations.forms
  • djblets.integrations.hooks
  • djblets.integrations.integration
  • djblets.integrations.manager
  • djblets.integrations.mixins
  • djblets.integrations.models
  • djblets.integrations.urls
  • djblets.integrations.views
  • djblets.log
  • djblets.log.middleware
  • djblets.log.siteconfig
  • djblets.log.urls
  • djblets.log.views
  • djblets.mail.dmarc
  • djblets.mail.message
  • djblets.mail.testing
  • djblets.mail.utils
  • djblets.markdown
  • djblets.markdown.extensions.escape_html
  • djblets.markdown.extensions.wysiwyg
  • djblets.markdown.extensions.wysiwyg_email
  • djblets.pipeline.compilers.es6
  • djblets.pipeline.compilers.less
  • djblets.pipeline.compilers.mixins
  • djblets.pipeline.compilers.rollup
  • djblets.pipeline.compilers.typescript
  • djblets.pipeline.settings
  • djblets.privacy.consent
  • djblets.privacy.consent.base
  • djblets.privacy.consent.common
  • djblets.privacy.consent.errors
  • djblets.privacy.consent.forms
  • djblets.privacy.consent.hooks
  • djblets.privacy.consent.registry
  • djblets.privacy.consent.tracker
  • djblets.privacy.models
  • djblets.privacy.pii
  • djblets.privacy.templatetags.djblets_privacy
  • djblets.protect
  • djblets.protect.locks
  • djblets.protect.ratelimit
  • djblets.recaptcha.mixins
  • djblets.recaptcha.siteconfig
  • djblets.recaptcha.templatetags.djblets_recaptcha
  • djblets.recaptcha.widgets
  • djblets.registries
  • djblets.registries.errors
  • djblets.registries.importer
  • djblets.registries.mixins
  • djblets.registries.registry
  • djblets.registries.signals
  • djblets.secrets
  • djblets.secrets.crypto
  • djblets.secrets.token_generators
  • djblets.secrets.token_generators.base
  • djblets.secrets.token_generators.legacy_sha1
  • djblets.secrets.token_generators.registry
  • djblets.secrets.token_generators.vendor_checksum
  • djblets.siteconfig
  • djblets.siteconfig.admin
  • djblets.siteconfig.context_processors
  • djblets.siteconfig.django_settings
  • djblets.siteconfig.forms
  • djblets.siteconfig.managers
  • djblets.siteconfig.middleware
  • djblets.siteconfig.models
  • djblets.siteconfig.signals
  • djblets.siteconfig.views
  • djblets.template.caches
  • djblets.template.context
  • djblets.template.loaders.conditional_cached
  • djblets.template.loaders.namespaced_app_dirs
  • djblets.testing.decorators
  • djblets.testing.testcases
  • djblets.testing.testrunners
  • djblets.urls.context_processors
  • djblets.urls.decorators
  • djblets.urls.patterns
  • djblets.urls.resolvers
  • djblets.urls.root
  • djblets.urls.staticfiles
  • djblets.util.compat.django.core.cache
  • djblets.util.compat.python.past
  • djblets.util.contextmanagers
  • djblets.util.dates
  • djblets.util.decorators
  • djblets.util.filesystem
  • djblets.util.functional
  • djblets.util.html
  • djblets.util.http
  • djblets.util.humanize
  • djblets.util.json_utils
  • djblets.util.properties
  • djblets.util.serializers
  • djblets.util.symbols
  • djblets.util.templatetags.djblets_deco
  • djblets.util.templatetags.djblets_email
  • djblets.util.templatetags.djblets_forms
  • djblets.util.templatetags.djblets_images
  • djblets.util.templatetags.djblets_js
  • djblets.util.templatetags.djblets_utils
  • djblets.util.typing
  • djblets.util.views
  • djblets.views.generic.base
  • djblets.views.generic.etag
  • djblets.webapi.auth
  • djblets.webapi.auth.backends
  • djblets.webapi.auth.backends.api_tokens
  • djblets.webapi.auth.backends.base
  • djblets.webapi.auth.backends.basic
  • djblets.webapi.auth.backends.oauth2_tokens
  • djblets.webapi.auth.views
  • djblets.webapi.decorators
  • djblets.webapi.encoders
  • djblets.webapi.errors
  • djblets.webapi.fields
  • djblets.webapi.managers
  • djblets.webapi.models
  • djblets.webapi.oauth2_scopes
  • djblets.webapi.resources
  • djblets.webapi.resources.base
  • djblets.webapi.resources.group
  • djblets.webapi.resources.registry
  • djblets.webapi.resources.root
  • djblets.webapi.resources.user
  • djblets.webapi.resources.mixins.api_tokens
  • djblets.webapi.resources.mixins.forms
  • djblets.webapi.resources.mixins.oauth2_tokens
  • djblets.webapi.resources.mixins.queries
  • djblets.webapi.responses
  • djblets.webapi.signals
  • djblets.webapi.testing
  • djblets.webapi.testing.decorators
  • djblets.webapi.testing.testcases
  • General Index
  • Python Module Index
  • Release Notes
  • Dynamic Page Injections¶

    Extensible products often need to add content to a page, such as <script> tags, CSS, notice banners, and navigation items. This needs to be factored into a page’s ETags so that older content doesn’t get stuck in a browser cache somewhere.

    The djblets.pagestate module makes this easy by offering the following:

    • Template-defined points where data can be injected.

    • A method for manually adding content to a template point.

    • Injector classes that let calls dynamically generate content for any template points.

    We’ll walk through how this works.

    Setting Up¶

    You’ll first need to enable the template tag library and middleware. Add the following to your project’s settings.py:

    INSTALLED_APPS
        ...,
        'djblets.pagestate',
        ...
    ]
    
    MIDDLEWARE = [
        ...
        'djblets.pagestate.middleware.PageStateMiddleware',
        ...
    ]
    

    Making a Template Dynamic¶

    To make a portion of your template dynamic, simply use the {% page_hook_point %} template tag from the djblets.pagestate.templatetags.djblets_pagestate template library, and give it a name.

    For example:

    {% load djblets_pagestate %}
    
    <html>
     <head>
      <title>Page Title</title>
      {% page_hook_point "scripts" %}
     </head>
     <body>
      ...
      {% page_hook_point "after-content" %}
     </body>
    </html>
    

    This defines two points:

    1. scripts

    2. after-content

    Both of these will be replaced with content injected either manually or from registered injectors.

    Manually Injecting Content¶

    During your request/response cycle, you can manually inject content into a template hook point.

    To do this:

    1. Fetch the PageState for the HttpRequest, using PageState.for_request(request).

    2. Call PageState.inject() with the content and optional ETag to inject.

    For example:

    from django.http import HttpRequest, HttpResponse
    from django.shortcuts import render
    from django.utils.html import mark_safe
    from djblets.pagestate.state import PageState
    
    def my_view(
        request: HttpRquest,
    ) -> HttpResponse:
        page_state = PageState.for_request(request)
    
        page_state.inject('scripts', {
            'content': mark_safe('<script>alert("hi!")</script>'),
        })
    
        page_state.inject('after-content', {
            'content': build_some_content_html(),
            'etag': build_some_content_etag(),
        });
    
        return render(request, 'base.html')
    

    These will be placed in their respective template hook points.

    Building Dynamic Injectors¶

    You don’t have to manually inject content in every view. If you have content that’s going to be common across pages, you can create an injector.

    Injectors are classes that adhere to PageStateInjectorProtocol. They register a unique injector_id and implement iter_page_state_data().

    They’re then registered in the djblets.pagestate.injectors. page_state_injectors.

    For example:

    from collections.abc import Iterator
    
    from django.http import HttpRequest
    from django.template import Context
    from django.utils.html import format_html
    from djblets.pagestate.injectors import page_state_injectors
    from djblets.pagestate.state import PageStateData
    
    
    class MyInjector:
        injector_id = 'my-injector'
    
        def iter_page_state_data(
            self,
            *,
            point_name: str,
            request: HttpRequest,
            context: Context,
        ) -> Iterator[PageStateData]
            if point_name == 'scripts':
                for i in range(10):
                    yield {
                        'content': format_html(
                            '<script>console.log("i = {}");</script>',
                            i),
                        'etag': str(i),
                    }
    
    page_state_injectors.register(MyInjector())
    

    This simple injector will add a series of console.log() statements for the scripts template hook point, generating them dynamically based on a range of numbers.

    In practice, you might use an injector to look up data from a database, a registry, or another source.

    Cache-Busting with ETags¶

    When injecting, it’s recommended to provide an ETag that differentiates that particular piece of content from another that it might generate.

    In the above example, we’re just using the string version of the number in the loop, which is safe if that’s the only part of the HTML that would change. You might want to include more information than that, such as a version identifier for the format of the HTML.

    If an ETag isn’t specified, the full content will be used for part of the ETag.

    If the resulting HTTP response includes an ETag, then the ETag data injected into the page state will be mixed into it, forming a new ETag. This ensures that the ETag always changes to reflect any injections.

    If the HTTP response does not include an ETag, the ETag data will not be included, in order to avoid unintentionally caching the full page (which may be dynamic in other ways).

    Keep up with the latest Review Board releases, security updates, and helpful information.

    About
    News
    Demo
    RBCommons Hosting
    Integrations
    Happy Users
    Support Options
    Documentation
    FAQ
    User Manual
    RBTools
    Administration Guide
    Power Pack
    Release Notes
    Downloads
    Review Board
    RBTools
    Djblets
    Power Pack
    Package Store
    PGP Signatures
    Contributing
    Bug Tracker
    Submit Patches
    Development Setup
    Wiki
    Follow Us
    Mailing Lists
    Reddit
    Twitter
    Mastodon
    Facebook
    YouTube

    Copyright © 2006-2026 Beanbag, Inc. All rights reserved.

    Terms of Service — Privacy Policy — AI Ethics Policy — Branding

    On this page

    • [Top]
    • Setting Up
    • Making a Template Dynamic
    • Manually Injecting Content
    • Building Dynamic Injectors
    • Cache-Busting with ETags