Query Parameters

Sync component properties to URL query parameters for shareable, bookmarkable state. List the properties to sync in query_params; their values are read from the URL on load and written back to it as they change.

class ProductFilter(Component):
    search: str = ""
    category: str = "all"

    query_params = ['search', 'category']
PREVIEW

Refresh the page and the state is restored straight from the URL.

class QueryDemo(Component):
    search: str = ""
    category: str = "all"
    tags: list = []                       # an array parameter

    # Every name here round-trips through the URL query string.
    query_params = ["search", "category", "tags"]

    def inline_template(self):
        return """
        <label>Search</label>
        <input lace:model.live="search">

        <label>Category</label>
        <select lace:model.live="category">...</select>

        <label>Tags</label>
        {# checkboxes share one list -> ?tags=new&tags=sale #}
        <input type="checkbox" lace:model.live="tags" value="new"> New
        <input type="checkbox" lace:model.live="tags" value="sale"> Sale
        <input type="checkbox" lace:model.live="tags" value="featured"> Featured
        """

URL aliases

To expose a property under a shorter or friendlier URL key, replace the bare name with a {"param": ..., "as": ...} entry. The property keeps its Python name; the URL uses the alias.

query_params = [
    {"param": "search_query", "as": "q"},   # ?q=... instead of ?search_query=...
    {"param": "sort", "as": "s"},
]
PREVIEW

The long names map to short keys: ?q=&s=new

class AliasDemo(Component):
    search_query: str = ""
    sort: str = "new"

    # Map property -> short URL key
    query_params = [
        {"param": "search_query", "as": "q"},
        {"param": "sort", "as": "s"},
    ]

    def inline_template(self):
        return """
        <div class="...">
            <input lace:model.live.debounce.300ms="search_query" class="...">
            <select lace:model.live="sort" class="...">...</select>
        </div>
        """

Validation

Query params come from untrusted URLs - validate them on mount with a rules() method (see Validation) and fall back to defaults if they're invalid.

from django import forms

class ValidatedFilter(Component):
    price_min: int = 0
    price_max: int = 1000

    query_params = ['price_min', 'price_max']

    def rules(self):
        return {
            'price_min': forms.IntegerField(min_value=0),
            'price_max': forms.IntegerField(min_value=0, max_value=10000),
        }

    def mount(self):
        if not self.validate():
            # Invalid URL params - reset to defaults
            self.price_min = 0
            self.price_max = 1000

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink