django-components 0.63__tar.gz → 0.65__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_components-0.63/src/django_components.egg-info → django_components-0.65}/PKG-INFO +57 -36
- {django_components-0.63 → django_components-0.65}/README.md +46 -23
- django_components-0.65/pyproject.toml +102 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/app_settings.py +1 -1
- {django_components-0.63 → django_components-0.65}/src/django_components/component.py +106 -151
- {django_components-0.63 → django_components-0.65}/src/django_components/middleware.py +4 -0
- django_components-0.65/src/django_components/slots.py +467 -0
- django_components-0.65/src/django_components/templatetags/component_tags.py +393 -0
- {django_components-0.63 → django_components-0.65/src/django_components.egg-info}/PKG-INFO +57 -36
- {django_components-0.63 → django_components-0.65}/src/django_components.egg-info/SOURCES.txt +1 -1
- {django_components-0.63 → django_components-0.65}/tests/test_component.py +18 -0
- {django_components-0.63 → django_components-0.65}/tests/test_settings.py +16 -1
- django_components-0.63/pyproject.toml +0 -57
- django_components-0.63/setup.py +0 -43
- django_components-0.63/src/django_components/templatetags/component_tags.py +0 -768
- {django_components-0.63 → django_components-0.65}/LICENSE +0 -0
- {django_components-0.63 → django_components-0.65}/setup.cfg +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/__init__.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/apps.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/component_registry.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/logger.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/management/__init__.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/py.typed +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/template_loader.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/types.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components/utils.py +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components.egg-info/requires.txt +0 -0
- {django_components-0.63 → django_components-0.65}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_autodiscover.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_component_as_view.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_context.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_dependency_rendering.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_registry.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_startcomponent_command.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/test_templatetags.py +0 -0
- {django_components-0.63 → django_components-0.65}/tests/testutils.py +0 -0
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.65
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
|
-
|
|
6
|
-
Author: Emil Stenström
|
|
7
|
-
Author-email: emil@emilstenstrom.se
|
|
5
|
+
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
8
6
|
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/EmilStenstrom/django-components/
|
|
9
8
|
Keywords: django,components,css,js,html
|
|
10
|
-
Classifier:
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
9
|
+
Classifier: Framework :: Django
|
|
17
10
|
Classifier: Framework :: Django :: 3.2
|
|
18
11
|
Classifier: Framework :: Django :: 4.0
|
|
19
12
|
Classifier: Framework :: Django :: 4.1
|
|
20
13
|
Classifier: Framework :: Django :: 4.2
|
|
21
14
|
Classifier: Framework :: Django :: 5.0
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
22
16
|
Classifier: Programming Language :: Python
|
|
23
17
|
Classifier: Programming Language :: Python :: 3
|
|
24
|
-
Classifier:
|
|
25
|
-
Classifier:
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Requires-Python: <4.0,>=3.8
|
|
26
24
|
Description-Content-Type: text/markdown
|
|
27
25
|
License-File: LICENSE
|
|
28
26
|
Requires-Dist: Django>=3.2
|
|
@@ -167,12 +165,10 @@ Read on to find out how to build your first component!
|
|
|
167
165
|
|
|
168
166
|
## Compatiblity
|
|
169
167
|
|
|
170
|
-
Django-components supports all
|
|
168
|
+
Django-components supports all supported combinations versions of [Django](https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django) and [Python](https://devguide.python.org/versions/#versions).
|
|
171
169
|
|
|
172
170
|
| Python version | Django version |
|
|
173
171
|
|----------------|--------------------------|
|
|
174
|
-
| 3.6 | 3.2 |
|
|
175
|
-
| 3.7 | 3.2 |
|
|
176
172
|
| 3.8 | 3.2, 4.0, 4.1, 4.2 |
|
|
177
173
|
| 3.9 | 3.2, 4.0, 4.1, 4.2 |
|
|
178
174
|
| 3.10 | 3.2, 4.0, 4.1, 4.2, 5.0 |
|
|
@@ -183,7 +179,19 @@ Django-components supports all <a href="https://docs.djangoproject.com/en/dev/fa
|
|
|
183
179
|
|
|
184
180
|
A component in django-components is the combination of four things: CSS, Javascript, a Django template, and some Python code to put them all together.
|
|
185
181
|
|
|
186
|
-
|
|
182
|
+
```
|
|
183
|
+
sampleproject/
|
|
184
|
+
├── calendarapp/
|
|
185
|
+
├── components/ 🆕
|
|
186
|
+
│ └── calendar/ 🆕
|
|
187
|
+
│ ├── calendar.py 🆕
|
|
188
|
+
│ ├── script.js 🆕
|
|
189
|
+
│ ├── style.css 🆕
|
|
190
|
+
│ └── template.html 🆕
|
|
191
|
+
├── sampleproject/
|
|
192
|
+
├── manage.py
|
|
193
|
+
└── requirements.txt
|
|
194
|
+
```
|
|
187
195
|
|
|
188
196
|
Start by creating empty files in the structure above.
|
|
189
197
|
|
|
@@ -209,7 +217,7 @@ Then you need a javascript file that specifies how you interact with this compon
|
|
|
209
217
|
Now you need a Django template for your component. Feel free to define more variables like `date` in this example. When creating an instance of this component we will send in the values for these variables. The template will be rendered with whatever template backend you've specified in your Django settings file.
|
|
210
218
|
|
|
211
219
|
```htmldjango
|
|
212
|
-
{# In a file called [project root]/components/calendar/
|
|
220
|
+
{# In a file called [project root]/components/calendar/template.html #}
|
|
213
221
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
214
222
|
```
|
|
215
223
|
|
|
@@ -225,7 +233,7 @@ from django_components import component
|
|
|
225
233
|
class Calendar(component.Component):
|
|
226
234
|
# Templates inside `[your apps]/components` dir and `[project root]/components` dir will be automatically found. To customize which template to use based on context
|
|
227
235
|
# you can override def get_template_name() instead of specifying the below variable.
|
|
228
|
-
template_name = "calendar.html"
|
|
236
|
+
template_name = "calendar/template.html"
|
|
229
237
|
|
|
230
238
|
# This component takes one parameter, a date string to show in the template
|
|
231
239
|
def get_context_data(self, date):
|
|
@@ -234,8 +242,8 @@ class Calendar(component.Component):
|
|
|
234
242
|
}
|
|
235
243
|
|
|
236
244
|
class Media:
|
|
237
|
-
css = "style.css"
|
|
238
|
-
js = "script.js"
|
|
245
|
+
css = "calendar/style.css"
|
|
246
|
+
js = "calendar/script.js"
|
|
239
247
|
```
|
|
240
248
|
|
|
241
249
|
And voilá!! We've created our first component.
|
|
@@ -266,11 +274,11 @@ The output from the above template will be:
|
|
|
266
274
|
<html>
|
|
267
275
|
<head>
|
|
268
276
|
<title>My example calendar</title>
|
|
269
|
-
<link href="style.css" type="text/css" media="all" rel="stylesheet">
|
|
277
|
+
<link href="/static/calendar/style.css" type="text/css" media="all" rel="stylesheet">
|
|
270
278
|
</head>
|
|
271
279
|
<body>
|
|
272
280
|
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
|
|
273
|
-
<script src="script.js"></script>
|
|
281
|
+
<script src="/static/calendar/script.js"></script>
|
|
274
282
|
</body>
|
|
275
283
|
<html>
|
|
276
284
|
```
|
|
@@ -284,7 +292,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
284
292
|
```python
|
|
285
293
|
# In a file called [project root]/components/calendar.py
|
|
286
294
|
from django_components import component
|
|
287
|
-
from django_components import types as t
|
|
295
|
+
from django_components import types as t
|
|
288
296
|
|
|
289
297
|
@component.register("calendar")
|
|
290
298
|
class Calendar(component.Component):
|
|
@@ -292,16 +300,16 @@ class Calendar(component.Component):
|
|
|
292
300
|
return {
|
|
293
301
|
"date": date,
|
|
294
302
|
}
|
|
295
|
-
|
|
303
|
+
|
|
296
304
|
template: t.django_html = """
|
|
297
305
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
298
306
|
"""
|
|
299
|
-
|
|
307
|
+
|
|
300
308
|
css: t.css = """
|
|
301
309
|
.calendar-component { width: 200px; background: pink; }
|
|
302
310
|
.calendar-component span { font-weight: bold; }
|
|
303
311
|
"""
|
|
304
|
-
|
|
312
|
+
|
|
305
313
|
js: t.js = """
|
|
306
314
|
(function(){
|
|
307
315
|
if (document.querySelector(".calendar-component")) {
|
|
@@ -313,7 +321,7 @@ class Calendar(component.Component):
|
|
|
313
321
|
|
|
314
322
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
315
323
|
|
|
316
|
-
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
324
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
317
325
|
|
|
318
326
|
## Using slots in templates
|
|
319
327
|
|
|
@@ -335,7 +343,7 @@ In the example below we introduce two block tags that work hand in hand to make
|
|
|
335
343
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
336
344
|
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.
|
|
337
345
|
|
|
338
|
-
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template,
|
|
346
|
+
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _template.html_.
|
|
339
347
|
|
|
340
348
|
```htmldjango
|
|
341
349
|
<div class="calendar-component">
|
|
@@ -445,7 +453,7 @@ This is fine too:
|
|
|
445
453
|
|
|
446
454
|
_New in version 0.34_
|
|
447
455
|
|
|
448
|
-
Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively.
|
|
456
|
+
Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively.
|
|
449
457
|
|
|
450
458
|
In addition, `Component` now has a `render_to_response` method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.
|
|
451
459
|
|
|
@@ -457,7 +465,7 @@ from django_components import component
|
|
|
457
465
|
|
|
458
466
|
@component.register("calendar")
|
|
459
467
|
class Calendar(component.Component):
|
|
460
|
-
|
|
468
|
+
|
|
461
469
|
template = """
|
|
462
470
|
<div class="calendar-component">
|
|
463
471
|
<div class="header">
|
|
@@ -468,7 +476,7 @@ class Calendar(component.Component):
|
|
|
468
476
|
</div>
|
|
469
477
|
</div>
|
|
470
478
|
"""
|
|
471
|
-
|
|
479
|
+
|
|
472
480
|
def get(self, request, *args, **kwargs):
|
|
473
481
|
context = {
|
|
474
482
|
"date": request.GET.get("date", "2020-06-06"),
|
|
@@ -484,7 +492,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
484
492
|
```python
|
|
485
493
|
# In a file called [project root]/components/urls.py
|
|
486
494
|
from django.urls import path
|
|
487
|
-
from calendar import Calendar
|
|
495
|
+
from calendar import Calendar
|
|
488
496
|
|
|
489
497
|
urlpatterns = [
|
|
490
498
|
path("calendar/", Calendar.as_view()),
|
|
@@ -846,14 +854,12 @@ pytest
|
|
|
846
854
|
The library is also tested across many versions of Python and Django. To run tests that way:
|
|
847
855
|
|
|
848
856
|
```sh
|
|
849
|
-
pyenv install -s 3.6
|
|
850
|
-
pyenv install -s 3.7
|
|
851
857
|
pyenv install -s 3.8
|
|
852
858
|
pyenv install -s 3.9
|
|
853
859
|
pyenv install -s 3.10
|
|
854
860
|
pyenv install -s 3.11
|
|
855
861
|
pyenv install -s 3.12
|
|
856
|
-
pyenv local 3.
|
|
862
|
+
pyenv local 3.8 3.9 3.10 3.11 3.12
|
|
857
863
|
tox -p
|
|
858
864
|
```
|
|
859
865
|
|
|
@@ -887,3 +893,18 @@ Use the [sampleproject](./sampleproject/) demo project to validate the changes:
|
|
|
887
893
|
Once the server is up, it should be available at <http://127.0.0.1:8000>.
|
|
888
894
|
|
|
889
895
|
To display individual components, add them to the `urls.py`, like in the case of <http://127.0.0.1:8000/greeting>
|
|
896
|
+
|
|
897
|
+
## Development guides
|
|
898
|
+
|
|
899
|
+
### Slot rendering flow
|
|
900
|
+
|
|
901
|
+
1. Flow starts when a template string is being parsed into Django Template instance.
|
|
902
|
+
|
|
903
|
+
2. When a `{% component %}` template tag is encountered, its body is searched for all `{% fill %}` nodes (explicit or implicit). and this is attached to the created `ComponentNode`.
|
|
904
|
+
|
|
905
|
+
See the implementation of `component` template tag for details.
|
|
906
|
+
|
|
907
|
+
3. Template rendering is a separate action from template parsing. When the template is being rendered, the `ComponentNode` creates an instance of the `Component` class and passes it the slot fills.
|
|
908
|
+
|
|
909
|
+
It's at this point when `Component.render` is called, and the slots are
|
|
910
|
+
rendered.
|
|
@@ -138,12 +138,10 @@ Read on to find out how to build your first component!
|
|
|
138
138
|
|
|
139
139
|
## Compatiblity
|
|
140
140
|
|
|
141
|
-
Django-components supports all
|
|
141
|
+
Django-components supports all supported combinations versions of [Django](https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django) and [Python](https://devguide.python.org/versions/#versions).
|
|
142
142
|
|
|
143
143
|
| Python version | Django version |
|
|
144
144
|
|----------------|--------------------------|
|
|
145
|
-
| 3.6 | 3.2 |
|
|
146
|
-
| 3.7 | 3.2 |
|
|
147
145
|
| 3.8 | 3.2, 4.0, 4.1, 4.2 |
|
|
148
146
|
| 3.9 | 3.2, 4.0, 4.1, 4.2 |
|
|
149
147
|
| 3.10 | 3.2, 4.0, 4.1, 4.2, 5.0 |
|
|
@@ -154,7 +152,19 @@ Django-components supports all <a href="https://docs.djangoproject.com/en/dev/fa
|
|
|
154
152
|
|
|
155
153
|
A component in django-components is the combination of four things: CSS, Javascript, a Django template, and some Python code to put them all together.
|
|
156
154
|
|
|
157
|
-
|
|
155
|
+
```
|
|
156
|
+
sampleproject/
|
|
157
|
+
├── calendarapp/
|
|
158
|
+
├── components/ 🆕
|
|
159
|
+
│ └── calendar/ 🆕
|
|
160
|
+
│ ├── calendar.py 🆕
|
|
161
|
+
│ ├── script.js 🆕
|
|
162
|
+
│ ├── style.css 🆕
|
|
163
|
+
│ └── template.html 🆕
|
|
164
|
+
├── sampleproject/
|
|
165
|
+
├── manage.py
|
|
166
|
+
└── requirements.txt
|
|
167
|
+
```
|
|
158
168
|
|
|
159
169
|
Start by creating empty files in the structure above.
|
|
160
170
|
|
|
@@ -180,7 +190,7 @@ Then you need a javascript file that specifies how you interact with this compon
|
|
|
180
190
|
Now you need a Django template for your component. Feel free to define more variables like `date` in this example. When creating an instance of this component we will send in the values for these variables. The template will be rendered with whatever template backend you've specified in your Django settings file.
|
|
181
191
|
|
|
182
192
|
```htmldjango
|
|
183
|
-
{# In a file called [project root]/components/calendar/
|
|
193
|
+
{# In a file called [project root]/components/calendar/template.html #}
|
|
184
194
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
185
195
|
```
|
|
186
196
|
|
|
@@ -196,7 +206,7 @@ from django_components import component
|
|
|
196
206
|
class Calendar(component.Component):
|
|
197
207
|
# Templates inside `[your apps]/components` dir and `[project root]/components` dir will be automatically found. To customize which template to use based on context
|
|
198
208
|
# you can override def get_template_name() instead of specifying the below variable.
|
|
199
|
-
template_name = "calendar.html"
|
|
209
|
+
template_name = "calendar/template.html"
|
|
200
210
|
|
|
201
211
|
# This component takes one parameter, a date string to show in the template
|
|
202
212
|
def get_context_data(self, date):
|
|
@@ -205,8 +215,8 @@ class Calendar(component.Component):
|
|
|
205
215
|
}
|
|
206
216
|
|
|
207
217
|
class Media:
|
|
208
|
-
css = "style.css"
|
|
209
|
-
js = "script.js"
|
|
218
|
+
css = "calendar/style.css"
|
|
219
|
+
js = "calendar/script.js"
|
|
210
220
|
```
|
|
211
221
|
|
|
212
222
|
And voilá!! We've created our first component.
|
|
@@ -237,11 +247,11 @@ The output from the above template will be:
|
|
|
237
247
|
<html>
|
|
238
248
|
<head>
|
|
239
249
|
<title>My example calendar</title>
|
|
240
|
-
<link href="style.css" type="text/css" media="all" rel="stylesheet">
|
|
250
|
+
<link href="/static/calendar/style.css" type="text/css" media="all" rel="stylesheet">
|
|
241
251
|
</head>
|
|
242
252
|
<body>
|
|
243
253
|
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
|
|
244
|
-
<script src="script.js"></script>
|
|
254
|
+
<script src="/static/calendar/script.js"></script>
|
|
245
255
|
</body>
|
|
246
256
|
<html>
|
|
247
257
|
```
|
|
@@ -255,7 +265,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
255
265
|
```python
|
|
256
266
|
# In a file called [project root]/components/calendar.py
|
|
257
267
|
from django_components import component
|
|
258
|
-
from django_components import types as t
|
|
268
|
+
from django_components import types as t
|
|
259
269
|
|
|
260
270
|
@component.register("calendar")
|
|
261
271
|
class Calendar(component.Component):
|
|
@@ -263,16 +273,16 @@ class Calendar(component.Component):
|
|
|
263
273
|
return {
|
|
264
274
|
"date": date,
|
|
265
275
|
}
|
|
266
|
-
|
|
276
|
+
|
|
267
277
|
template: t.django_html = """
|
|
268
278
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
269
279
|
"""
|
|
270
|
-
|
|
280
|
+
|
|
271
281
|
css: t.css = """
|
|
272
282
|
.calendar-component { width: 200px; background: pink; }
|
|
273
283
|
.calendar-component span { font-weight: bold; }
|
|
274
284
|
"""
|
|
275
|
-
|
|
285
|
+
|
|
276
286
|
js: t.js = """
|
|
277
287
|
(function(){
|
|
278
288
|
if (document.querySelector(".calendar-component")) {
|
|
@@ -284,7 +294,7 @@ class Calendar(component.Component):
|
|
|
284
294
|
|
|
285
295
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
286
296
|
|
|
287
|
-
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
297
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
288
298
|
|
|
289
299
|
## Using slots in templates
|
|
290
300
|
|
|
@@ -306,7 +316,7 @@ In the example below we introduce two block tags that work hand in hand to make
|
|
|
306
316
|
- `{% slot <name> %}`/`{% endslot %}`: Declares a new slot in the component template.
|
|
307
317
|
- `{% fill <name> %}`/`{% endfill %}`: (Used inside a `component` tag pair.) Fills a declared slot with the specified content.
|
|
308
318
|
|
|
309
|
-
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template,
|
|
319
|
+
Let's update our calendar component to support more customization. We'll add `slot` tag pairs to its template, _template.html_.
|
|
310
320
|
|
|
311
321
|
```htmldjango
|
|
312
322
|
<div class="calendar-component">
|
|
@@ -416,7 +426,7 @@ This is fine too:
|
|
|
416
426
|
|
|
417
427
|
_New in version 0.34_
|
|
418
428
|
|
|
419
|
-
Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively.
|
|
429
|
+
Components can now be used as views. To do this, `Component` subclasses Django's `View` class. This means that you can use all of the [methods](https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#view) of `View` in your component. For example, you can override `get` and `post` to handle GET and POST requests, respectively.
|
|
420
430
|
|
|
421
431
|
In addition, `Component` now has a `render_to_response` method that renders the component template based on the provided context and slots' data and returns an `HttpResponse` object.
|
|
422
432
|
|
|
@@ -428,7 +438,7 @@ from django_components import component
|
|
|
428
438
|
|
|
429
439
|
@component.register("calendar")
|
|
430
440
|
class Calendar(component.Component):
|
|
431
|
-
|
|
441
|
+
|
|
432
442
|
template = """
|
|
433
443
|
<div class="calendar-component">
|
|
434
444
|
<div class="header">
|
|
@@ -439,7 +449,7 @@ class Calendar(component.Component):
|
|
|
439
449
|
</div>
|
|
440
450
|
</div>
|
|
441
451
|
"""
|
|
442
|
-
|
|
452
|
+
|
|
443
453
|
def get(self, request, *args, **kwargs):
|
|
444
454
|
context = {
|
|
445
455
|
"date": request.GET.get("date", "2020-06-06"),
|
|
@@ -455,7 +465,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
455
465
|
```python
|
|
456
466
|
# In a file called [project root]/components/urls.py
|
|
457
467
|
from django.urls import path
|
|
458
|
-
from calendar import Calendar
|
|
468
|
+
from calendar import Calendar
|
|
459
469
|
|
|
460
470
|
urlpatterns = [
|
|
461
471
|
path("calendar/", Calendar.as_view()),
|
|
@@ -817,14 +827,12 @@ pytest
|
|
|
817
827
|
The library is also tested across many versions of Python and Django. To run tests that way:
|
|
818
828
|
|
|
819
829
|
```sh
|
|
820
|
-
pyenv install -s 3.6
|
|
821
|
-
pyenv install -s 3.7
|
|
822
830
|
pyenv install -s 3.8
|
|
823
831
|
pyenv install -s 3.9
|
|
824
832
|
pyenv install -s 3.10
|
|
825
833
|
pyenv install -s 3.11
|
|
826
834
|
pyenv install -s 3.12
|
|
827
|
-
pyenv local 3.
|
|
835
|
+
pyenv local 3.8 3.9 3.10 3.11 3.12
|
|
828
836
|
tox -p
|
|
829
837
|
```
|
|
830
838
|
|
|
@@ -858,3 +866,18 @@ Use the [sampleproject](./sampleproject/) demo project to validate the changes:
|
|
|
858
866
|
Once the server is up, it should be available at <http://127.0.0.1:8000>.
|
|
859
867
|
|
|
860
868
|
To display individual components, add them to the `urls.py`, like in the case of <http://127.0.0.1:8000/greeting>
|
|
869
|
+
|
|
870
|
+
## Development guides
|
|
871
|
+
|
|
872
|
+
### Slot rendering flow
|
|
873
|
+
|
|
874
|
+
1. Flow starts when a template string is being parsed into Django Template instance.
|
|
875
|
+
|
|
876
|
+
2. When a `{% component %}` template tag is encountered, its body is searched for all `{% fill %}` nodes (explicit or implicit). and this is attached to the created `ComponentNode`.
|
|
877
|
+
|
|
878
|
+
See the implementation of `component` template tag for details.
|
|
879
|
+
|
|
880
|
+
3. Template rendering is a separate action from template parsing. When the template is being rendered, the `ComponentNode` creates an instance of the `Component` class and passes it the slot fills.
|
|
881
|
+
|
|
882
|
+
It's at this point when `Component.render` is called, and the slots are
|
|
883
|
+
rendered.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django_components"
|
|
7
|
+
version = "0.65"
|
|
8
|
+
requires-python = ">=3.8, <4.0"
|
|
9
|
+
description = "A way to create simple reusable template components in Django."
|
|
10
|
+
keywords = ["django", "components", "css", "js", "html"]
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Emil Stenström", email = "emil@emilstenstrom.se"},
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Framework :: Django",
|
|
17
|
+
"Framework :: Django :: 3.2",
|
|
18
|
+
"Framework :: Django :: 4.0",
|
|
19
|
+
"Framework :: Django :: 4.1",
|
|
20
|
+
"Framework :: Django :: 4.2",
|
|
21
|
+
"Framework :: Django :: 5.0",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Programming Language :: Python",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.8",
|
|
26
|
+
"Programming Language :: Python :: 3.9",
|
|
27
|
+
"Programming Language :: Python :: 3.10",
|
|
28
|
+
"Programming Language :: Python :: 3.11",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
]
|
|
31
|
+
dependencies = [
|
|
32
|
+
'Django>=3.2',
|
|
33
|
+
]
|
|
34
|
+
license = {text = "MIT"}
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/EmilStenstrom/django-components/"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.packages.find]
|
|
41
|
+
where = ["src"]
|
|
42
|
+
include = ["django_components*"]
|
|
43
|
+
exclude = ["django_components.tests*"]
|
|
44
|
+
namespaces = false
|
|
45
|
+
|
|
46
|
+
[tool.black]
|
|
47
|
+
line-length = 119
|
|
48
|
+
include = '\.pyi?$'
|
|
49
|
+
exclude = '''
|
|
50
|
+
/(
|
|
51
|
+
\.git
|
|
52
|
+
| \.hg
|
|
53
|
+
| \.mypy_cache
|
|
54
|
+
| \.tox
|
|
55
|
+
| \.venv
|
|
56
|
+
| activate
|
|
57
|
+
| _build
|
|
58
|
+
| buck-out
|
|
59
|
+
| build
|
|
60
|
+
| dist
|
|
61
|
+
)/
|
|
62
|
+
'''
|
|
63
|
+
|
|
64
|
+
[tool.isort]
|
|
65
|
+
profile = "black"
|
|
66
|
+
line_length = 119
|
|
67
|
+
multi_line_output = 3
|
|
68
|
+
include_trailing_comma = "True"
|
|
69
|
+
known_first_party = "django_components"
|
|
70
|
+
|
|
71
|
+
[tool.flake8]
|
|
72
|
+
ignore = ['E302', 'W503']
|
|
73
|
+
max-line-length = 119
|
|
74
|
+
exclude = [
|
|
75
|
+
'migrations',
|
|
76
|
+
'__pycache__',
|
|
77
|
+
'manage.py',
|
|
78
|
+
'settings.py',
|
|
79
|
+
'env',
|
|
80
|
+
'.env',
|
|
81
|
+
'.venv',
|
|
82
|
+
'.tox',
|
|
83
|
+
'build',
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
[tool.mypy]
|
|
87
|
+
check_untyped_defs = true
|
|
88
|
+
ignore_missing_imports = true
|
|
89
|
+
exclude = [
|
|
90
|
+
'test_structures',
|
|
91
|
+
'build',
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
[[tool.mypy.overrides]]
|
|
95
|
+
module = "django_components.*"
|
|
96
|
+
disallow_untyped_defs = true
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
[tool.pytest.ini_options]
|
|
100
|
+
testpaths = [
|
|
101
|
+
"tests"
|
|
102
|
+
]
|