What's Changed
🚨📢 Deprecation
-
Currently, view request handlers such as
get()andpost()methods can be defined
directly on theComponentclass:class MyComponent(Component): def get(self, request): return self.render_to_response()
Or, nested within the
Component.Viewclass:class MyComponent(Component): class View: def get(self, request): return self.render_to_response()
This first approach is now deprecated, and will be removed in v1. Instead, the
get(),put()and similar methods should be defined within theComponent.Viewclass.
Feat
-
Each Component class now has a
class_idattribute, which is unique to the component subclass.NOTE: This is different from
Component.id, which is unique to each rendered instance.To look up a component class by its
class_id, useget_component_by_class_id(). -
It's now easier to create URLs for component views.
Before, you had to call
Component.as_view()and pass that tourlpatterns.Now this can be done for you if you set
Component.Url.publictoTrue:class MyComponent(Component): class Url: public = True ...
Then, to get the URL for the component, use
get_component_url():from django_components import get_component_url url = get_component_url(MyComponent)
This way you don't have to mix your app URLs with component URLs.
Read more on Component views and URLs.
-
Per-component caching - Set
Component.Cache.enabledtoTrueto enable caching for a component.Component caching allows you to store the rendered output of a component. Next time the component is rendered
with the same input, the cached output is returned instead of re-rendering the component.class TestComponent(Component): template = "Hello" class Cache: enabled = True ttl = 0.1 # .1 seconds TTL cache_name = "custom_cache" # Custom hash method for args and kwargs # NOTE: The default implementation simply serializes the input into a string. # As such, it might not be suitable for complex objects like Models. def hash(self, *args, **kwargs): return f"{json.dumps(args)}:{json.dumps(kwargs)}"
Read more on Component caching.
-
@djc_testcan now be called without first callingdjango.setup(), in which case it does it for you. -
Expose
ComponentInputclass, which is a typing forComponent.input.
Refactor
Component.get_context_data()can now omit a return statement or returnNone.
Full Changelog: 0.136...0.137