django-components/sampleproject/components/greeting.py
Juro Oravec f100cc1836
Some checks are pending
Docs - build & deploy / docs (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 / test_sampleproject (3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.10) (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
refactor: replace isort, black and flake8 with ruff (#1346)
2025-09-10 14:06:53 +02:00

39 lines
1.1 KiB
Python

from django.http import HttpRequest, HttpResponse
from django_components import Component, register, types
@register("greeting")
class Greeting(Component):
template: types.django_html = """
<div id="greeting">Hello, {{ name }}!</div>
{% slot "message" %}{% endslot %}
"""
css: types.css = """
#greeting {
display: inline-block;
color: blue;
font-size: 2em;
}
"""
js: types.js = """
document.getElementById("greeting").addEventListener("click", (event) => {
alert("Hello!");
});
"""
def get_template_data(self, args, kwargs, slots, context):
return {"name": kwargs["name"]}
class View:
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
slots = {"message": "Hello, world!"}
return Greeting.render_to_response(
request=request,
slots=slots,
kwargs={
"name": request.GET.get("name", ""),
},
)