django-components/sampleproject/components/greeting.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

39 lines
1 KiB
Python

from typing import Any, Dict
from django_components import Component, register, types
@register("greeting")
class Greeting(Component):
class View:
def get(self, request, *args, **kwargs):
slots = {"message": "Hello, world!"}
return Greeting.render_to_response(
request=request,
slots=slots,
kwargs={
"name": request.GET.get("name", ""),
},
)
def get_context_data(self, name, *args, **kwargs) -> Dict[str, Any]:
return {"name": name}
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!");
});
"""