@@ -34,8 +34,6 @@ If you do not use the built-in components, you do not need to add them to your `
34
34
.. code-block :: python
35
35
36
36
INSTALLED_APPS = [
37
- # Optional dependencies
38
- ' djangocms_icon' ,
39
37
' easy_thumbnails' ,
40
38
' djangocms_link' , # Required if djangocms_frontend.contrib.link is used
41
39
# Main frontend app - built-in components from contrib not needed
@@ -67,7 +65,7 @@ as required for ``djangocms-frontend`` template components.
67
65
Directory Structure
68
66
-------------------
69
67
70
- The templte component lives in the template directory of any of your apps.
68
+ The template component lives in the template directory of any of your apps.
71
69
Ensure your DjangoCMS app has the following structure::
72
70
73
71
theme_app/
@@ -117,7 +115,10 @@ Then, add the following code::
117
115
{% childplugins %}{% endchildplugins %}
118
116
</div>
119
117
<div class="hidden lg:mt-0 lg:col-span-5 lg:flex">
120
- <img src="{{ hero_image.url }}">
118
+ {# Get the related object of the image field which itself is just a dict #}
119
+ {% with image=instance.hero_image|get_related_object %}
120
+ <img src="{{ image.url }}" alt="{{ image.alt }}">
121
+ {% endwith %}
121
122
</div>
122
123
</div>
123
124
</section>
@@ -133,7 +134,7 @@ Component Declaration
133
134
{% cms_component "Hero" name=_("My Hero Component") %}
134
135
135
136
This tag **declares ** the component and assigns it a name (``Hero ``). This is used internally
136
- by django CMS to identify the plguin later. The ``name `` parameter is used to display the
137
+ by django CMS to identify the plugin later. The ``name `` parameter is used to display the
137
138
component in the CMS admin interface. Internally the command declares a ``CMSFrontendComponent ``
138
139
class. All named arguments are added to the component's Meta class.
139
140
@@ -205,27 +206,36 @@ content, i.e. add/remove ``{% field %}`` tags, or change the ``{% cms_component
205
206
the Django server to apply the changes.
206
207
207
208
1. Restart your Django server.
208
- 2. Create a new page end edit it.
209
+ 2. Create a new page And edit it.
209
210
3. Add a new **Hero component ** to a page from the plugin picker.
210
211
4. Fill in the **title **, **slogan **, and **hero image ** fields.
211
212
5. Save and publish the page.
212
213
213
214
Using the component in your templates
214
215
-------------------------------------
215
216
216
- To use the component in your templates, you can use the ``{% plugin %} `` tag with the component's name.
217
- For example, to render the **Hero component ** in a template, use the following code::
217
+ To use the component in your templates outside django CMS , you can use the ``{% plugin %} `` tag with the
218
+ component's name. For example, to render the **Hero component ** in a template, use the following code::
218
219
219
220
{% load frontend %}
220
221
{% plugin "hero" title=_("Welcome to my new website") slogan=_("Building successful websites since 1896") %}
221
222
223
+ .. note ::
224
+ Do not forget to register the component with :attr: `CMS_COMPONENT_PLUGINS `. If you needed to list the single
225
+ component in the setting, the hero component's dotted path to its plugin would be
226
+ ``djangocms_frontend.cms_plugins.HeroPlugin ``.
227
+
222
228
223
229
Adding inline-editing to the component
224
230
--------------------------------------
225
231
226
- When using `djangocms-text <https://github.com/django-cms/djangocms-text >`_, fields of the component can be
227
- marked as inline fields to activate inline editing. Simply replace ``{{ title }} `` and/or ``{{ slogan }} `` with
228
- ``{% inline_field "title" %} `` and/or ``{% inline_field "slogan" %} ``::
232
+ When using `djangocms-text <https://github.com/django-cms/djangocms-text >`_, `CharField ` and `HTMLFormField ` fields
233
+ of the component can be marked as inline fields to activate inline editing. Inline-editing fields can be changed in
234
+ the edit endpoint by simply clicking inside and typing over the text - without the need to open an edit dialogue for
235
+ the component.
236
+
237
+ Simply replace ``{{ title }} `` and/or ``{{ slogan }} `` with ``{% inline_field "title" %} `` and/or
238
+ ``{% inline_field "slogan" %} ``::
229
239
230
240
<h1>{% inline_field "title" %}</h1>
231
241
<p>{% inline_field "slogan" %}</p>
@@ -253,12 +263,44 @@ Template components are a powerful tool for developers, but they have some limit
253
263
Classes are intantiated by default, for example. This is ok for ``widget=forms.Textarea ``, but potentially not
254
264
for more complex cases.
255
265
266
+
267
+ Trouble Shooting
268
+ ================
269
+
270
+ If the component does not appear in the plugin picker, check the following:
271
+
272
+ 1. **INSTALLED_APPS **: Verify that the app containing the component is listed in your ``INSTALLED_APPS `` setting.
273
+
274
+ 2. **Template Location **: Ensure the template file is located in the correct directory structure:
275
+ ``templates/<app_name>/cms_components/ `` inside your app.
276
+
277
+ 3. **Server Restart **: Restart the Django server after creating or modifying the component template. Changes in
278
+ the declarative part are only reflected after server restart.
279
+
280
+ 4. **Rendering exceptions **: The template component will only be added if it renders without exception. Make
281
+ sure it does not fail if the context is empty. Check the server logs for errors during startup. Missing
282
+ dependencies or syntax errors in the template can prevent the component from being registered.
283
+
284
+ 5. **Migration module **: Make sure the app has a migration module. If not, create one with
285
+ ``python -m manage makemigrations <app_name> ``.
286
+
287
+ 6. **Permissions **: Add the necessary permissions for the user/group if you are not the superuser.
288
+ Also see :ref: `sync_permissions `.
289
+
290
+ If the issue persists, double-check the template syntax and ensure all required fields are properly defined.
291
+
256
292
Conclusion
257
293
==========
258
294
259
- You have successfully created a **djangocms-frontend template component ** using ``cms_component ``!
260
- This structure allows editors to easily customize hero sections while maintaining a reusable
261
- and structured design.
295
+ In this tutorial, you learned how to create a reusable **Hero component ** using ``djangocms-frontend ``.
296
+ This approach allows you to:
297
+
298
+ - Simplify component creation for editors by offering inline editing.
299
+ - Maintain consistent design across your website by reusing the component.
300
+ - Extend functionality without writing Python code.
301
+
302
+ By following these steps, you can create additional components tailored to your project's needs.
303
+
262
304
263
305
.. note ::
264
306
0 commit comments