django-components 0.90__tar.gz → 0.92__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {django_components-0.90/src/django_components.egg-info → django_components-0.92}/PKG-INFO +173 -6
  2. {django_components-0.90 → django_components-0.92}/README.md +172 -5
  3. {django_components-0.90 → django_components-0.92}/pyproject.toml +1 -1
  4. {django_components-0.90 → django_components-0.92}/src/django_components/__init__.py +4 -1
  5. {django_components-0.90 → django_components-0.92}/src/django_components/attributes.py +2 -2
  6. {django_components-0.90 → django_components-0.92}/src/django_components/component.py +149 -38
  7. {django_components-0.90 → django_components-0.92}/src/django_components/slots.py +3 -3
  8. {django_components-0.90 → django_components-0.92/src/django_components.egg-info}/PKG-INFO +173 -6
  9. {django_components-0.90 → django_components-0.92}/tests/test_attributes.py +28 -0
  10. {django_components-0.90 → django_components-0.92}/tests/test_component.py +83 -1
  11. {django_components-0.90 → django_components-0.92}/tests/test_component_as_view.py +59 -11
  12. {django_components-0.90 → django_components-0.92}/LICENSE +0 -0
  13. {django_components-0.90 → django_components-0.92}/setup.cfg +0 -0
  14. {django_components-0.90 → django_components-0.92}/src/django_components/app_settings.py +0 -0
  15. {django_components-0.90 → django_components-0.92}/src/django_components/apps.py +0 -0
  16. {django_components-0.90 → django_components-0.92}/src/django_components/autodiscover.py +0 -0
  17. {django_components-0.90 → django_components-0.92}/src/django_components/component_media.py +0 -0
  18. {django_components-0.90 → django_components-0.92}/src/django_components/component_registry.py +0 -0
  19. {django_components-0.90 → django_components-0.92}/src/django_components/context.py +0 -0
  20. {django_components-0.90 → django_components-0.92}/src/django_components/expression.py +0 -0
  21. {django_components-0.90 → django_components-0.92}/src/django_components/library.py +0 -0
  22. {django_components-0.90 → django_components-0.92}/src/django_components/logger.py +0 -0
  23. {django_components-0.90 → django_components-0.92}/src/django_components/management/__init__.py +0 -0
  24. {django_components-0.90 → django_components-0.92}/src/django_components/management/commands/__init__.py +0 -0
  25. {django_components-0.90 → django_components-0.92}/src/django_components/management/commands/startcomponent.py +0 -0
  26. {django_components-0.90 → django_components-0.92}/src/django_components/management/commands/upgradecomponent.py +0 -0
  27. {django_components-0.90 → django_components-0.92}/src/django_components/middleware.py +0 -0
  28. {django_components-0.90 → django_components-0.92}/src/django_components/node.py +0 -0
  29. {django_components-0.90 → django_components-0.92}/src/django_components/provide.py +0 -0
  30. {django_components-0.90 → django_components-0.92}/src/django_components/py.typed +0 -0
  31. {django_components-0.90 → django_components-0.92}/src/django_components/safer_staticfiles/__init__.py +0 -0
  32. {django_components-0.90 → django_components-0.92}/src/django_components/safer_staticfiles/apps.py +0 -0
  33. {django_components-0.90 → django_components-0.92}/src/django_components/tag_formatter.py +0 -0
  34. {django_components-0.90 → django_components-0.92}/src/django_components/template_loader.py +0 -0
  35. {django_components-0.90 → django_components-0.92}/src/django_components/template_parser.py +0 -0
  36. {django_components-0.90 → django_components-0.92}/src/django_components/templatetags/__init__.py +0 -0
  37. {django_components-0.90 → django_components-0.92}/src/django_components/templatetags/component_tags.py +0 -0
  38. {django_components-0.90 → django_components-0.92}/src/django_components/types.py +0 -0
  39. {django_components-0.90 → django_components-0.92}/src/django_components/utils.py +0 -0
  40. {django_components-0.90 → django_components-0.92}/src/django_components.egg-info/SOURCES.txt +0 -0
  41. {django_components-0.90 → django_components-0.92}/src/django_components.egg-info/dependency_links.txt +0 -0
  42. {django_components-0.90 → django_components-0.92}/src/django_components.egg-info/requires.txt +0 -0
  43. {django_components-0.90 → django_components-0.92}/src/django_components.egg-info/top_level.txt +0 -0
  44. {django_components-0.90 → django_components-0.92}/tests/test_autodiscover.py +0 -0
  45. {django_components-0.90 → django_components-0.92}/tests/test_command_startcomponent.py +0 -0
  46. {django_components-0.90 → django_components-0.92}/tests/test_component_media.py +0 -0
  47. {django_components-0.90 → django_components-0.92}/tests/test_context.py +0 -0
  48. {django_components-0.90 → django_components-0.92}/tests/test_dependency_rendering.py +0 -0
  49. {django_components-0.90 → django_components-0.92}/tests/test_registry.py +0 -0
  50. {django_components-0.90 → django_components-0.92}/tests/test_safer_staticfiles.py +0 -0
  51. {django_components-0.90 → django_components-0.92}/tests/test_settings.py +0 -0
  52. {django_components-0.90 → django_components-0.92}/tests/test_tag_formatter.py +0 -0
  53. {django_components-0.90 → django_components-0.92}/tests/test_template_loader.py +0 -0
  54. {django_components-0.90 → django_components-0.92}/tests/test_template_parser.py +0 -0
  55. {django_components-0.90 → django_components-0.92}/tests/test_templatetags.py +0 -0
  56. {django_components-0.90 → django_components-0.92}/tests/test_templatetags_component.py +0 -0
  57. {django_components-0.90 → django_components-0.92}/tests/test_templatetags_provide.py +0 -0
  58. {django_components-0.90 → django_components-0.92}/tests/test_templatetags_slot_fill.py +0 -0
  59. {django_components-0.90 → django_components-0.92}/tests/test_templatetags_templating.py +0 -0
  60. {django_components-0.90 → django_components-0.92}/tests/test_utils.py +0 -0
  61. {django_components-0.90 → django_components-0.92}/tests/testutils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django_components
3
- Version: 0.90
3
+ Version: 0.92
4
4
  Summary: A way to create simple reusable template components in Django.
5
5
  Author-email: Emil Stenström <emil@emilstenstrom.se>
6
6
  License: MIT
@@ -73,6 +73,13 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
73
73
 
74
74
  ## Release notes
75
75
 
76
+ 🚨📢 **Version 0.92**
77
+ - BREAKING CHANGE: `Component` class is no longer a subclass of `View`. To configure the `View` class, set the `Component.View` nested class. HTTP methods like `get` or `post` can still be defined directly on `Component` class, and `Component.as_view()` internally calls `Component.View.as_view()`. (See [Modifying the View class](#modifying-the-view-class))
78
+
79
+ - The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `get_template_string` and `get_template_name` via `self.input`. (See [Accessing data passed to the component](#accessing-data-passed-to-the-component))
80
+
81
+ - Typing: `Component` class supports generics that specify types for `Component.render` (See [Adding type hints with Generics](#adding-type-hints-with-generics))
82
+
76
83
  **Version 0.90**
77
84
  - All tags (`component`, `slot`, `fill`, ...) now support "self-closing" or "inline" form, where you can omit the closing tag:
78
85
  ```django
@@ -612,6 +619,52 @@ MyComponent.render_to_response(
612
619
  )
613
620
  ```
614
621
 
622
+ ### Adding type hints with Generics
623
+
624
+ The `Component` class optionally accepts type parameters
625
+ that allow you to specify the types of args, kwargs, slots, and
626
+ data.
627
+
628
+ ```py
629
+ from typing import NotRequired, Tuple, TypedDict
630
+
631
+ # Tuple
632
+ Args = Tuple[int, str]
633
+
634
+ # Mapping
635
+ class Kwargs(TypedDict):
636
+ variable: str
637
+ another: int
638
+ maybe_var: NotRequired[int]
639
+
640
+ # Mapping
641
+ class Data(TypedDict):
642
+ variable: str
643
+
644
+ # Mapping
645
+ class Slots(TypedDict):
646
+ my_slot: NotRequired[str]
647
+
648
+ class Button(Component[Args, Kwargs, Data, Slots]):
649
+ def get_context_data(self, variable, another):
650
+ return {
651
+ "variable": variable,
652
+ }
653
+ ```
654
+
655
+ When you then call `Component.render` or `Component.render_to_response`, you will get type hints:
656
+
657
+ ```py
658
+ Button.render(
659
+ # Error: First arg must be `int`, got `float`
660
+ args=(1.25, "abc"),
661
+ # Error: Key "another" is missing
662
+ kwargs={
663
+ "variable": "text",
664
+ },
665
+ )
666
+ ```
667
+
615
668
  ### Response class of `render_to_response`
616
669
 
617
670
  While `render` method returns a plain string, `render_to_response` wraps the rendered content in a "Response" class. By default, this is `django.http.HttpResponse`.
@@ -638,15 +691,22 @@ assert isinstance(response, MyResponse)
638
691
 
639
692
  _New in version 0.34_
640
693
 
641
- Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively.
694
+ _Note: Since 0.92, Component no longer subclasses View. To configure the View class, set the nested `Component.View` class_
695
+
696
+ Components can now be used as views:
697
+ - Components define the `Component.as_view()` class method that can be used the same as [`View.as_view()`](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#django.views.generic.base.View.as_view).
698
+
699
+ - By default, you can define GET, POST or other HTTP handlers directly on the Component, same as you do with [View](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#view). For example, you can override `get` and `post` to handle GET and POST requests, respectively.
700
+
701
+ - In addition, `Component` now has a [`render_to_response`](#inputs-of-render-and-render_to_response) method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.
642
702
 
643
- In addition, `Component` now has a [`render_to_response`](#inputs-of-render-and-render_to_response) method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.
703
+ ### Component as view example
644
704
 
645
705
  Here's an example of a calendar component defined as a view:
646
706
 
647
707
  ```python
648
708
  # In a file called [project root]/components/calendar.py
649
- from django_components import Component, register
709
+ from django_components import Component, ComponentView, register
650
710
 
651
711
  @register("calendar")
652
712
  class Calendar(Component):
@@ -662,6 +722,7 @@ class Calendar(Component):
662
722
  </div>
663
723
  """
664
724
 
725
+ # Handle GET requests
665
726
  def get(self, request, *args, **kwargs):
666
727
  context = {
667
728
  "date": request.GET.get("date", "2020-06-06"),
@@ -669,7 +730,11 @@ class Calendar(Component):
669
730
  slots = {
670
731
  "header": "Calendar header",
671
732
  }
672
- return self.render_to_response(context=context, slots=slots)
733
+ # Return HttpResponse with the rendered content
734
+ return self.render_to_response(
735
+ context=context,
736
+ slots=slots,
737
+ )
673
738
  ```
674
739
 
675
740
  Then, to use this component as a view, you should create a `urls.py` file in your components directory, and add a path to the component's view:
@@ -684,6 +749,9 @@ urlpatterns = [
684
749
  ]
685
750
  ```
686
751
 
752
+ `Component.as_view()` is a shorthand for calling [`View.as_view()`](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#django.views.generic.base.View.as_view) and passing the component
753
+ instance as one of the arguments.
754
+
687
755
  Remember to add `__init__.py` to your components directory, so that Django can find the `urls.py` file.
688
756
 
689
757
  Finally, include the component's urls in your project's `urls.py` file:
@@ -697,10 +765,77 @@ urlpatterns = [
697
765
  ]
698
766
  ```
699
767
 
700
- Note: slots content are automatically escaped by default to prevent XSS attacks. To disable escaping, set `escape_slots_content=False` in the `render_to_response` method. If you do so, you should make sure that any content you pass to the slots is safe, especially if it comes from user input.
768
+ Note: Slots content are automatically escaped by default to prevent XSS attacks. To disable escaping, set `escape_slots_content=False` in the `render_to_response` method. If you do so, you should make sure that any content you pass to the slots is safe, especially if it comes from user input.
701
769
 
702
770
  If you're planning on passing an HTML string, check Django's use of [`format_html`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.html.format_html) and [`mark_safe`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.safestring.mark_safe).
703
771
 
772
+ ### Modifying the View class
773
+
774
+ The View class that handles the requests is defined on `Component.View`.
775
+
776
+ When you define a GET or POST handlers on the `Component` class, like so:
777
+
778
+ ```py
779
+ class MyComponent(Component):
780
+ def get(self, request, *args, **kwargs):
781
+ return self.render_to_response(
782
+ context={
783
+ "date": request.GET.get("date", "2020-06-06"),
784
+ },
785
+ )
786
+
787
+ def post(self, request, *args, **kwargs) -> HttpResponse:
788
+ variable = request.POST.get("variable")
789
+ return self.render_to_response(
790
+ kwargs={"variable": variable}
791
+ )
792
+ ```
793
+
794
+ Then the request is still handled by `Component.View.get()` or `Component.View.post()`
795
+ methods. However, by default, `Component.View.get()` points to `Component.get()`, and so on.
796
+
797
+ ```py
798
+ class ComponentView(View):
799
+ component: Component = None
800
+ ...
801
+
802
+ def get(self, request, *args, **kwargs):
803
+ return self.component.get(request, *args, **kwargs)
804
+
805
+ def post(self, request, *args, **kwargs):
806
+ return self.component.post(request, *args, **kwargs)
807
+
808
+ ...
809
+ ```
810
+
811
+ If you want to define your own `View` class, you need to:
812
+ 1. Set the class as `Component.View`
813
+ 2. Subclass from `ComponentView`, so the View instance has access to the component class.
814
+
815
+ In the example below, we added extra logic into `View.setup()`.
816
+
817
+ Note that the POST handler is still defined at the top. This is because `View` subclasses `ComponentView`, which defines the `post()` method that calls `Component.post()`.
818
+
819
+ If you were to overwrite the `View.post()` method, then `Component.post()` would be ignored.
820
+
821
+ ```py
822
+ from django_components import Component, ComponentView
823
+
824
+ class MyComponent(Component):
825
+
826
+ def post(self, request, *args, **kwargs) -> HttpResponse:
827
+ variable = request.POST.get("variable")
828
+ return self.component.render_to_response(
829
+ kwargs={"variable": variable}
830
+ )
831
+
832
+ class View(ComponentView):
833
+ def setup(self, request, *args, **kwargs):
834
+ super(request, *args, **kwargs)
835
+
836
+ do_something_extra(request, *args, **kwargs)
837
+ ```
838
+
704
839
  ## Registering components
705
840
 
706
841
  In previous examples you could repeatedly see us using `@register()` to "register"
@@ -1413,6 +1548,38 @@ Sweet! Now all the relevant HTML is inside the template, and we can move it to a
1413
1548
  > {"attrs": {"my_key:two": 2}}
1414
1549
  > ```
1415
1550
 
1551
+ ### Accessing data passed to the component
1552
+
1553
+ When you call `Component.render` or `Component.render_to_response`, the inputs to these methods can be accessed from within the instance under `self.input`.
1554
+
1555
+ This means that you can use `self.input` inside:
1556
+ - `get_context_data`
1557
+ - `get_template_name`
1558
+ - `get_template_string`
1559
+
1560
+ `self.input` is defined only for the duration of `Component.render`, and returns `None` when called outside of this.
1561
+
1562
+ `self.input` has the same fields as the input to `Component.render`:
1563
+
1564
+ ```py
1565
+ class TestComponent(Component):
1566
+ def get_context_data(self, var1, var2, variable, another, **attrs):
1567
+ assert self.input.args == (123, "str")
1568
+ assert self.input.kwargs == {"variable": "test", "another": 1}
1569
+ assert self.input.slots == {"my_slot": "MY_SLOT"}
1570
+ assert isinstance(self.input.context, Context)
1571
+
1572
+ return {
1573
+ "variable": variable,
1574
+ }
1575
+
1576
+ rendered = TestComponent.render(
1577
+ kwargs={"variable": "test", "another": 1},
1578
+ args=(123, "str"),
1579
+ slots={"my_slot": "MY_SLOT"},
1580
+ )
1581
+ ```
1582
+
1416
1583
  ## Rendering HTML attributes
1417
1584
 
1418
1585
  _New in version 0.74_:
@@ -49,6 +49,13 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
49
49
 
50
50
  ## Release notes
51
51
 
52
+ 🚨📢 **Version 0.92**
53
+ - BREAKING CHANGE: `Component` class is no longer a subclass of `View`. To configure the `View` class, set the `Component.View` nested class. HTTP methods like `get` or `post` can still be defined directly on `Component` class, and `Component.as_view()` internally calls `Component.View.as_view()`. (See [Modifying the View class](#modifying-the-view-class))
54
+
55
+ - The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `get_template_string` and `get_template_name` via `self.input`. (See [Accessing data passed to the component](#accessing-data-passed-to-the-component))
56
+
57
+ - Typing: `Component` class supports generics that specify types for `Component.render` (See [Adding type hints with Generics](#adding-type-hints-with-generics))
58
+
52
59
  **Version 0.90**
53
60
  - All tags (`component`, `slot`, `fill`, ...) now support "self-closing" or "inline" form, where you can omit the closing tag:
54
61
  ```django
@@ -588,6 +595,52 @@ MyComponent.render_to_response(
588
595
  )
589
596
  ```
590
597
 
598
+ ### Adding type hints with Generics
599
+
600
+ The `Component` class optionally accepts type parameters
601
+ that allow you to specify the types of args, kwargs, slots, and
602
+ data.
603
+
604
+ ```py
605
+ from typing import NotRequired, Tuple, TypedDict
606
+
607
+ # Tuple
608
+ Args = Tuple[int, str]
609
+
610
+ # Mapping
611
+ class Kwargs(TypedDict):
612
+ variable: str
613
+ another: int
614
+ maybe_var: NotRequired[int]
615
+
616
+ # Mapping
617
+ class Data(TypedDict):
618
+ variable: str
619
+
620
+ # Mapping
621
+ class Slots(TypedDict):
622
+ my_slot: NotRequired[str]
623
+
624
+ class Button(Component[Args, Kwargs, Data, Slots]):
625
+ def get_context_data(self, variable, another):
626
+ return {
627
+ "variable": variable,
628
+ }
629
+ ```
630
+
631
+ When you then call `Component.render` or `Component.render_to_response`, you will get type hints:
632
+
633
+ ```py
634
+ Button.render(
635
+ # Error: First arg must be `int`, got `float`
636
+ args=(1.25, "abc"),
637
+ # Error: Key "another" is missing
638
+ kwargs={
639
+ "variable": "text",
640
+ },
641
+ )
642
+ ```
643
+
591
644
  ### Response class of `render_to_response`
592
645
 
593
646
  While `render` method returns a plain string, `render_to_response` wraps the rendered content in a "Response" class. By default, this is `django.http.HttpResponse`.
@@ -614,15 +667,22 @@ assert isinstance(response, MyResponse)
614
667
 
615
668
  _New in version 0.34_
616
669
 
617
- Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively.
670
+ _Note: Since 0.92, Component no longer subclasses View. To configure the View class, set the nested `Component.View` class_
671
+
672
+ Components can now be used as views:
673
+ - Components define the `Component.as_view()` class method that can be used the same as [`View.as_view()`](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#django.views.generic.base.View.as_view).
674
+
675
+ - By default, you can define GET, POST or other HTTP handlers directly on the Component, same as you do with [View](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#view). For example, you can override `get` and `post` to handle GET and POST requests, respectively.
676
+
677
+ - In addition, `Component` now has a [`render_to_response`](#inputs-of-render-and-render_to_response) method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.
618
678
 
619
- In addition, `Component` now has a [`render_to_response`](#inputs-of-render-and-render_to_response) method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.
679
+ ### Component as view example
620
680
 
621
681
  Here's an example of a calendar component defined as a view:
622
682
 
623
683
  ```python
624
684
  # In a file called [project root]/components/calendar.py
625
- from django_components import Component, register
685
+ from django_components import Component, ComponentView, register
626
686
 
627
687
  @register("calendar")
628
688
  class Calendar(Component):
@@ -638,6 +698,7 @@ class Calendar(Component):
638
698
  </div>
639
699
  """
640
700
 
701
+ # Handle GET requests
641
702
  def get(self, request, *args, **kwargs):
642
703
  context = {
643
704
  "date": request.GET.get("date", "2020-06-06"),
@@ -645,7 +706,11 @@ class Calendar(Component):
645
706
  slots = {
646
707
  "header": "Calendar header",
647
708
  }
648
- return self.render_to_response(context=context, slots=slots)
709
+ # Return HttpResponse with the rendered content
710
+ return self.render_to_response(
711
+ context=context,
712
+ slots=slots,
713
+ )
649
714
  ```
650
715
 
651
716
  Then, to use this component as a view, you should create a `urls.py` file in your components directory, and add a path to the component's view:
@@ -660,6 +725,9 @@ urlpatterns = [
660
725
  ]
661
726
  ```
662
727
 
728
+ `Component.as_view()` is a shorthand for calling [`View.as_view()`](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#django.views.generic.base.View.as_view) and passing the component
729
+ instance as one of the arguments.
730
+
663
731
  Remember to add `__init__.py` to your components directory, so that Django can find the `urls.py` file.
664
732
 
665
733
  Finally, include the component's urls in your project's `urls.py` file:
@@ -673,10 +741,77 @@ urlpatterns = [
673
741
  ]
674
742
  ```
675
743
 
676
- Note: slots content are automatically escaped by default to prevent XSS attacks. To disable escaping, set `escape_slots_content=False` in the `render_to_response` method. If you do so, you should make sure that any content you pass to the slots is safe, especially if it comes from user input.
744
+ Note: Slots content are automatically escaped by default to prevent XSS attacks. To disable escaping, set `escape_slots_content=False` in the `render_to_response` method. If you do so, you should make sure that any content you pass to the slots is safe, especially if it comes from user input.
677
745
 
678
746
  If you're planning on passing an HTML string, check Django's use of [`format_html`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.html.format_html) and [`mark_safe`](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.safestring.mark_safe).
679
747
 
748
+ ### Modifying the View class
749
+
750
+ The View class that handles the requests is defined on `Component.View`.
751
+
752
+ When you define a GET or POST handlers on the `Component` class, like so:
753
+
754
+ ```py
755
+ class MyComponent(Component):
756
+ def get(self, request, *args, **kwargs):
757
+ return self.render_to_response(
758
+ context={
759
+ "date": request.GET.get("date", "2020-06-06"),
760
+ },
761
+ )
762
+
763
+ def post(self, request, *args, **kwargs) -> HttpResponse:
764
+ variable = request.POST.get("variable")
765
+ return self.render_to_response(
766
+ kwargs={"variable": variable}
767
+ )
768
+ ```
769
+
770
+ Then the request is still handled by `Component.View.get()` or `Component.View.post()`
771
+ methods. However, by default, `Component.View.get()` points to `Component.get()`, and so on.
772
+
773
+ ```py
774
+ class ComponentView(View):
775
+ component: Component = None
776
+ ...
777
+
778
+ def get(self, request, *args, **kwargs):
779
+ return self.component.get(request, *args, **kwargs)
780
+
781
+ def post(self, request, *args, **kwargs):
782
+ return self.component.post(request, *args, **kwargs)
783
+
784
+ ...
785
+ ```
786
+
787
+ If you want to define your own `View` class, you need to:
788
+ 1. Set the class as `Component.View`
789
+ 2. Subclass from `ComponentView`, so the View instance has access to the component class.
790
+
791
+ In the example below, we added extra logic into `View.setup()`.
792
+
793
+ Note that the POST handler is still defined at the top. This is because `View` subclasses `ComponentView`, which defines the `post()` method that calls `Component.post()`.
794
+
795
+ If you were to overwrite the `View.post()` method, then `Component.post()` would be ignored.
796
+
797
+ ```py
798
+ from django_components import Component, ComponentView
799
+
800
+ class MyComponent(Component):
801
+
802
+ def post(self, request, *args, **kwargs) -> HttpResponse:
803
+ variable = request.POST.get("variable")
804
+ return self.component.render_to_response(
805
+ kwargs={"variable": variable}
806
+ )
807
+
808
+ class View(ComponentView):
809
+ def setup(self, request, *args, **kwargs):
810
+ super(request, *args, **kwargs)
811
+
812
+ do_something_extra(request, *args, **kwargs)
813
+ ```
814
+
680
815
  ## Registering components
681
816
 
682
817
  In previous examples you could repeatedly see us using `@register()` to "register"
@@ -1389,6 +1524,38 @@ Sweet! Now all the relevant HTML is inside the template, and we can move it to a
1389
1524
  > {"attrs": {"my_key:two": 2}}
1390
1525
  > ```
1391
1526
 
1527
+ ### Accessing data passed to the component
1528
+
1529
+ When you call `Component.render` or `Component.render_to_response`, the inputs to these methods can be accessed from within the instance under `self.input`.
1530
+
1531
+ This means that you can use `self.input` inside:
1532
+ - `get_context_data`
1533
+ - `get_template_name`
1534
+ - `get_template_string`
1535
+
1536
+ `self.input` is defined only for the duration of `Component.render`, and returns `None` when called outside of this.
1537
+
1538
+ `self.input` has the same fields as the input to `Component.render`:
1539
+
1540
+ ```py
1541
+ class TestComponent(Component):
1542
+ def get_context_data(self, var1, var2, variable, another, **attrs):
1543
+ assert self.input.args == (123, "str")
1544
+ assert self.input.kwargs == {"variable": "test", "another": 1}
1545
+ assert self.input.slots == {"my_slot": "MY_SLOT"}
1546
+ assert isinstance(self.input.context, Context)
1547
+
1548
+ return {
1549
+ "variable": variable,
1550
+ }
1551
+
1552
+ rendered = TestComponent.render(
1553
+ kwargs={"variable": "test", "another": 1},
1554
+ args=(123, "str"),
1555
+ slots={"my_slot": "MY_SLOT"},
1556
+ )
1557
+ ```
1558
+
1392
1559
  ## Rendering HTML attributes
1393
1560
 
1394
1561
  _New in version 0.74_:
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "django_components"
7
- version = "0.90"
7
+ version = "0.92"
8
8
  requires-python = ">=3.8, <4.0"
9
9
  description = "A way to create simple reusable template components in Django."
10
10
  keywords = ["django", "components", "css", "js", "html"]
@@ -7,7 +7,10 @@ from django_components.autodiscover import (
7
7
  autodiscover as autodiscover,
8
8
  import_libraries as import_libraries,
9
9
  )
10
- from django_components.component import Component as Component
10
+ from django_components.component import (
11
+ Component as Component,
12
+ ComponentView as ComponentView,
13
+ )
11
14
  from django_components.component_registry import (
12
15
  AlreadyRegistered as AlreadyRegistered,
13
16
  ComponentRegistry as ComponentRegistry,
@@ -33,8 +33,8 @@ class HtmlAttrsNode(Node):
33
33
  resolved_value = safe_resolve(value, context)
34
34
  append_attrs.append((key, resolved_value))
35
35
 
36
- defaults = safe_resolve(self.defaults, context) if self.defaults else {}
37
- attrs = safe_resolve(self.attributes, context) if self.attributes else {}
36
+ defaults = safe_resolve(self.defaults, context) or {} if self.defaults else {}
37
+ attrs = safe_resolve(self.attributes, context) or {} if self.attributes else {}
38
38
 
39
39
  # Merge it
40
40
  final_attrs = {**defaults, **attrs}