django-components/tests/components/single_file.py
Juro Oravec a49f5e51dd
feat: component URL (#1088)
* feat: allow to set defaults

* refactor: remove input validation and link to it

* feat: component URL

* refactor: fix linter errors

* refactor: fix linter errors + update examples to use Component.View..get

* docs: update comment

* refactor: revert change to hash_comp_cls

* docs: update comment
2025-04-07 10:44:41 +02:00

33 lines
1 KiB
Python

from typing import Any, Dict
from django.http import HttpResponse
from django_components import Component, register, types
@register("single_file_component")
class SingleFileComponent(Component):
template: types.django_html = """
<form method="post">
{% csrf_token %}
<input type="text" name="variable" value="{{ variable }}">
<input type="submit">
</form>
"""
class View:
def post(self, request, *args, **kwargs) -> HttpResponse:
variable = request.POST.get("variable")
return SingleFileComponent.render_to_response(
request=request,
kwargs={"variable": variable},
)
def get(self, request, *args, **kwargs) -> HttpResponse:
return SingleFileComponent.render_to_response(
request=request,
kwargs={"variable": "GET"},
)
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
return {"variable": variable}