EmilStenstrom / django-components

Create simple reusable template components in Django.
MIT License
1.16k stars 76 forks source link

I cant seem to get django-components working #321

Closed baseplate-admin closed 1 year ago

baseplate-admin commented 1 year ago

Hi thanks for this awesome libaray, But i cant seem to get it working.

Here's the PR with Changes : https://github.com/baseplate-admin/CoreProject/pull/2725/commits/43956143324fa98537285d8a237366aa2b6fb997

And here's the error i face:

image

What could be the reason behind this

baseplate-admin commented 1 year ago

Relavant sections :

https://github.com/baseplate-admin/CoreProject/blob/43956143324fa98537285d8a237366aa2b6fb997/backend/django_core/core/settings.py#L79-L80

https://github.com/baseplate-admin/CoreProject/blob/43956143324fa98537285d8a237366aa2b6fb997/backend/django_core/core/settings.py#L207-L226

https://github.com/baseplate-admin/CoreProject/blob/43956143324fa98537285d8a237366aa2b6fb997/backend/django_core/core/settings.py#L334

baseplate-admin commented 1 year ago

Stacktrace :

Internal Server Error: /anime/
Traceback (most recent call last):
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Programming\coreproject-elixir\CoreProject\backend\django_core\apps\pages\views\anime.py", line 31, in anime_home_view
    return render(request, "anime/home.html", context={"icons": icons})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\test\utils.py", line 112, in instrumented_test_render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\loader_tags.py", line 157, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\test\utils.py", line 112, in instrumented_test_render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django_components\templatetags\component_tags.py", line 422, in render
    component_cls: Type[Component] = component_registry.get(
                                     ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baseplate-admin\AppData\Local\pypoetry\Cache\virtualenvs\coreproject-1UeyYTYF-py3.11\Lib\site-packages\django_components\component_registry.py", line 28, in get
    raise NotRegistered('The component "%s" is not registered' % name)
django_components.component_registry.NotRegistered: The component "vercel_hover" is not registered
EmilStenstrom commented 1 year ago

Hi! Thanks for providing enough context to properly help you find the problem.

All the setup seems to work like it should (the component tag is found for instance). Instead, the problem is that your vercel_hover component isn't found. The code for finding the components is here: https://github.com/EmilStenstrom/django-components/blob/master/django_components/__init__.py#L17

As you see, it looks for a component directory in each app you have, and otherwise falls back to your "libraries" setting which you can find docs for here: https://github.com/EmilStenstrom/django-components#configure-the-module-where-components-are-loaded-from

All in all, I think the solution is as simple as pointing to your components dir with the libraries settings.

Can you get it working?

baseplate-admin commented 1 year ago

Yep, Sorry for late reply.

I see the error.

image

This structure works

While this one doesn't

image

Is it possible to specify the parent directory for components?

Thanks a bunch

EmilStenstrom commented 1 year ago

So, did you try the libraries setting i mentioned above?

baseplate-admin commented 1 year ago

So, did you try the libraries setting i mentioned above?

Yep i did, Specifically this. It works !

But if i have hundreds of modules, it would be a bit tedious to load them one by one, no?

baseplate-admin commented 1 year ago

This issue is resolved. Thanks for your help @EmilStenstrom