mirror of
https://github.com/django-components/django-components.git
synced 2025-11-14 20:49:41 +00:00
refactor: make Component.View.public optional (#1451)
Some checks are pending
Docs - build & deploy / docs (push) Waiting to run
Run tests / build (windows-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.11) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.12) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.8) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.11) (push) Waiting to run
Run tests / build (windows-latest, 3.12) (push) Waiting to run
Run tests / build (windows-latest, 3.13) (push) Waiting to run
Run tests / build (windows-latest, 3.8) (push) Waiting to run
Run tests / build (windows-latest, 3.9) (push) Waiting to run
Run tests / test_docs (3.13) (push) Waiting to run
Run tests / test_sampleproject (3.13) (push) Waiting to run
Some checks are pending
Docs - build & deploy / docs (push) Waiting to run
Run tests / build (windows-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.11) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.12) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.8) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.11) (push) Waiting to run
Run tests / build (windows-latest, 3.12) (push) Waiting to run
Run tests / build (windows-latest, 3.13) (push) Waiting to run
Run tests / build (windows-latest, 3.8) (push) Waiting to run
Run tests / build (windows-latest, 3.9) (push) Waiting to run
Run tests / test_docs (3.13) (push) Waiting to run
Run tests / test_sampleproject (3.13) (push) Waiting to run
Co-authored-by: RohanDisa <105740583+RohanDisa@users.noreply.github.com>
This commit is contained in:
parent
b17214f536
commit
485027b308
11 changed files with 202 additions and 71 deletions
|
|
@ -13,7 +13,9 @@ django-components has a suite of features that help you write and manage views a
|
|||
|
||||
- Use [`Component.as_view()`](../../../reference/api#django_components.Component.as_view) to be able to use your Components with Django's [`urlpatterns`](https://docs.djangoproject.com/en/5.2/topics/http/urls/). This works the same way as [`View.as_view()`](https://docs.djangoproject.com/en/5.2/ref/class-based-views/base/#django.views.generic.base.View.as_view).
|
||||
|
||||
- To avoid having to manually define the endpoints for each component, you can set the component to be "public" with [`Component.View.public = True`](../../../reference/api#django_components.ComponentView.public). This will automatically create a URL for the component. To retrieve the component URL, use [`get_component_url()`](../../../reference/api#django_components.get_component_url).
|
||||
- Or use [`get_component_url()`](../../../reference/api#django_components.get_component_url) to retrieve the component URL - an anonymous HTTP endpoint that triggers the component's handlers without having to register the component in `urlpatterns`.
|
||||
|
||||
To expose a component, simply define a handler, or explicitly expose/hide the component with [`Component.View.public = True/False`](../../../reference/api#django_components.ComponentView.public).
|
||||
|
||||
- In addition, [`Component`](../../../reference/api#django_components.Component) has a [`render_to_response()`](../../../reference/api#django_components.Component.render_to_response) method that renders the component template based on the provided input and returns an `HttpResponse` object.
|
||||
|
||||
|
|
@ -114,19 +116,13 @@ class as one of the arguments.
|
|||
|
||||
## Register URLs automatically
|
||||
|
||||
If you don't care about the exact URL of the component, you can let django-components manage the URLs for you by setting the [`Component.View.public`](../../../reference/api#django_components.ComponentView.public) attribute to `True`:
|
||||
If you don't care about the exact URL of the component, you can let django-components manage the URLs.
|
||||
|
||||
```py
|
||||
class MyComponent(Component):
|
||||
class View:
|
||||
public = True
|
||||
Each component has an "anonymous" URL that triggers the component's HTTP handlers without having to define the component in `urlpatterns`.
|
||||
|
||||
def get(self, request):
|
||||
return self.component_cls.render_to_response(request=request)
|
||||
...
|
||||
```
|
||||
This way you don't have to mix your app URLs with component URLs.
|
||||
|
||||
Then, to get the URL for the component, use [`get_component_url()`](../../../reference/api#django_components.get_component_url):
|
||||
To obtain such "anonymous" URL, use [`get_component_url()`](../../../reference/api#django_components.get_component_url):
|
||||
|
||||
```py
|
||||
from django_components import get_component_url
|
||||
|
|
@ -134,7 +130,17 @@ 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.
|
||||
The component is automatically registered in `urlpatterns` when you define a handler. You can also explicitly expose/hide the component with [`Component.View.public`](../../../reference/api#django_components.ComponentView.public):
|
||||
|
||||
```py
|
||||
class MyComponent(Component):
|
||||
class View:
|
||||
public = False
|
||||
|
||||
def get(self, request):
|
||||
return self.component_cls.render_to_response(request=request)
|
||||
...
|
||||
```
|
||||
|
||||
!!! info
|
||||
|
||||
|
|
|
|||
|
|
@ -55,8 +55,6 @@ class ContactFormComponent(Component):
|
|||
""" # noqa: E501
|
||||
|
||||
class View:
|
||||
public = True
|
||||
|
||||
# Submit handler
|
||||
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
||||
# Access the submitted data
|
||||
|
|
|
|||
|
|
@ -121,8 +121,6 @@ class FragmentsPage(Component):
|
|||
"""
|
||||
|
||||
class View:
|
||||
public = True
|
||||
|
||||
# The same GET endpoint handles rendering either the whole page or a fragment.
|
||||
# We use the `type` query parameter to determine which one to render.
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
|
|
|
|||
|
|
@ -261,18 +261,22 @@ Next, you need to set the URL for the component.
|
|||
|
||||
You can either:
|
||||
|
||||
1. Automatically assign the URL by setting the [`Component.View.public`](../../reference/api#django_components.ComponentView.public) attribute to `True`.
|
||||
1. Use [`get_component_url()`](../../reference/api#django_components.get_component_url) to retrieve the component URL - an anonymous HTTP endpoint that triggers the component's handlers without having to register the component in `urlpatterns`.
|
||||
|
||||
In this case, use [`get_component_url()`](../../reference/api#django_components.get_component_url) to get the URL for the component view.
|
||||
```py
|
||||
from django_components import get_component_url
|
||||
|
||||
url = get_component_url(Calendar)
|
||||
```
|
||||
|
||||
The component endpoint is automatically registered in `urlpatterns` when you define a handler. To explicitly expose/hide the component, use [`Component.View.public`](../../../reference/api#django_components.ComponentView.public).
|
||||
|
||||
```djc_py
|
||||
from django_components import Component, get_component_url
|
||||
from django_components import Component
|
||||
|
||||
class Calendar(Component):
|
||||
class View:
|
||||
public = True
|
||||
|
||||
url = get_component_url(Calendar)
|
||||
public = False
|
||||
```
|
||||
|
||||
2. Manually assign the URL by setting [`Component.as_view()`](../../reference/api#django_components.Component.as_view) to your `urlpatterns`:
|
||||
|
|
|
|||
|
|
@ -335,13 +335,10 @@ class Calendar(Component):
|
|||
template_file = "calendar.html"
|
||||
|
||||
class View:
|
||||
# Register Component with `urlpatterns`
|
||||
public = True
|
||||
|
||||
# Define handlers
|
||||
def get(self, request, *args, **kwargs):
|
||||
page = request.GET.get("page", 1)
|
||||
return self.component.render_to_response(
|
||||
return Calendar.render_to_response(
|
||||
request=request,
|
||||
kwargs={
|
||||
"page": page,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue