{"id":15673173,"url":"https://github.com/andreyfedoseev/django-page-components","last_synced_at":"2025-08-10T14:15:33.692Z","repository":{"id":44603176,"uuid":"120796560","full_name":"andreyfedoseev/django-page-components","owner":"andreyfedoseev","description":"\"Page component\" is a unit of a user interface (think ReactJS components). django-page-components provide a minimalistic framework for creating page components and using them in your Django views and templates.","archived":false,"fork":false,"pushed_at":"2022-02-05T11:59:32.000Z","size":19,"stargazers_count":16,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-02T05:47:25.526Z","etag":null,"topics":["css","django","javascript","templates","ui-components","views"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreyfedoseev.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-08T17:52:28.000Z","updated_at":"2024-11-04T12:48:55.000Z","dependencies_parsed_at":"2022-08-28T16:20:27.677Z","dependency_job_id":null,"html_url":"https://github.com/andreyfedoseev/django-page-components","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/andreyfedoseev/django-page-components","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreyfedoseev%2Fdjango-page-components","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreyfedoseev%2Fdjango-page-components/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreyfedoseev%2Fdjango-page-components/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreyfedoseev%2Fdjango-page-components/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreyfedoseev","download_url":"https://codeload.github.com/andreyfedoseev/django-page-components/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreyfedoseev%2Fdjango-page-components/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269734160,"owners_count":24466557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","django","javascript","templates","ui-components","views"],"created_at":"2024-10-03T15:38:06.586Z","updated_at":"2025-08-10T14:15:33.657Z","avatar_url":"https://github.com/andreyfedoseev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"======================\ndjango-page-components\n======================\n\n.. image:: https://github.com/andreyfedoseev/django-page-components/actions/workflows/main.yml/badge.svg\n    :target: https://github.com/andreyfedoseev/django-page-components/actions/workflows/main.yml\n    :alt: Tests Status\n\n\n\"Page component\" is a unit of a user interface (think ReactJS components). ``django-page-components`` provide\na minimalistic framework for creating page components and using them in your Django views and templates.\n\nTo define a page component, you need to create a sub-class of ``page_components.PageComponent``\nand implement ``render`` method like so:\n\n.. code-block:: python\n\n    import page_components\n    import django.utils.html\n\n\n    class AddToCartButton(page_components.PageComponent):\n\n        def __init__(self, product):\n            self.product = product\n\n        class Media:\n            js = (\n                \"add-to-cart.js\",  # this is where addToCart is defined\n            )\n            css = {\n                \"all\": (\n                    \"add-to-cart.css\"  # this is where `.add-to-card` styles are defined\n                )\n            }\n\n        def render(self):\n            return django.utils.html.format_html(\n                \"\"\"\u003cbutton class=\"add-to-cart\" onclick=\"addToCart({ product_id })\"\u003eAdd to cart\u003c/button\u003e\"\"\",\n                product_id=self.product.id\n            )\n\n\nYou can also use a ``TemplatePageComponent`` base class to implement page components based on templates.\nIn that case, you may want to implement ``get_context_data`` method:\n\n.. code-block:: python\n\n    class AddToCartButton(page_components.TemplatePageComponent):\n\n        template_name = \"add-to-cart-button.html\"\n\n        ...\n\n        def get_context_data(self, **kwargs):\n            kwargs[\"product_id\"] = self.product_id\n            return super(AddToCartButton, self).get_context_data(**kwargs)\n\nNote that it's up to you to decide how to implement the ``render`` method and what additional methods should be added\nto your page components. One general recommendation is to keep the ``__init__`` method as lightweight as possible and do\nall the heavy lifting in the ``render`` method.\n\nA proposed convention is to store your page components classes in ``page_components`` package/module inside your app::\n\n    myapp.page_components.AddToCartButton\n\nNow, when we have some page components defined, it is time to use them in views:\n\n.. code-block:: python\n\n    import django.views.generic\n    import page_components\n\n    import myapp.models\n    import myapp.page_components\n\n    class ProductPage(\n        page_components.PageComponentsView,\n        django.views.generic.DetailView,\n    ):\n\n        model = myapp.models.Product\n        template_name = \"product.html\"\n\n        def get_page_components(self):\n            return {\n                \"add_to_cart_button\": myapp.page_components.AddToCartButton(self.object)\n            }\n\n\nand templates:\n\n.. code-block:: html\n\n    \u003chtml\u003e\n      \u003chead\u003e\n        /* this will include CSS files for all page components on that page */\n        {{ view.media.css.render }}\n      \u003c/head\u003e\n      \u003cbody\u003e\n        \u003ch1\u003e{{ object.title }}\u003c/h1\u003e\n        {{ page_components.add_to_cart_button }}\n\n        /* this will include JavaScript files for all page components on that page */\n        {{ view.media.js.render }}\n      \u003c/body\u003e\n    \u003c/html\u003e\n\nNote that page components are placed to ``page_components`` namespace in template context by default. You can change\nthat namespace on per-view basis by adding ``page_components_context_name`` attribute to a view class or globally with\n``PAGE_COMPONENTS_CONTEXT_NAME`` setting. If you set ``page_components_context_name`` to ``None``, it will disable\nthe namespace entirely.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreyfedoseev%2Fdjango-page-components","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreyfedoseev%2Fdjango-page-components","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreyfedoseev%2Fdjango-page-components/lists"}