Code Monkey home page Code Monkey logo

Comments (11)

EmilStenstrom avatar EmilStenstrom commented on May 29, 2024 1

Thanks to hard work of @Real-Gecko (THANKS!) this is no released as part of 0.17.

from django-components.

EmilStenstrom avatar EmilStenstrom commented on May 29, 2024

Hi! What happens if you add the calendars directory to the DIRS setting? Does that solve the TemplateDoesNotExist error?

from django-components.

oaltun avatar oaltun commented on May 29, 2024

It did work with the following arrangement.

files:
tmp/components.py
tmp/components/calendar/static/calendar/script.js
tmp/components/calendar/static/calendar/style.css
tmp/components/calendar/templates/calendar.html

components.py

from django_components import component

@component.register("calendar")
class Calendar(component.Component):
    def context(self, date):
        return {
            "date": date,
        }

    def template(self, context):
        return "calendar/calendar.html"

    class Media:
        css = 'calendar/style.css'
        js = 'calendar/script.js'

templates setting:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            str(APPS_DIR / 'templates'),
            str(APPS_DIR / 'tmp' / 'components' / 'calendar' / 'templates')
        ], # APPS_DIR is ../mysite

stitic files dirs setting:

STATICFILES_DIRS = [
    str(APPS_DIR / 'static'),
    str(APPS_DIR / 'tmp' / 'components' / 'calendar' / 'static'),
]

I guess somebody needs to write a template loader and a static files finder for django to automatically find these files.

from django-components.

EmilStenstrom avatar EmilStenstrom commented on May 29, 2024

Thanks for posting the solution!

No matter what, you somehow need to specify the path to your templates. Are you suggesting we should come up with a default location for components, and make a loader that automatically loads templates from there?

from django-components.

oaltun avatar oaltun commented on May 29, 2024

Right, default locations like <app_name>/components/<component_name>/templates for templates and <app_name>/components/<component_name>/static for static files would be nice I think.

from django-components.

Real-Gecko avatar Real-Gecko commented on May 29, 2024

I've encountered the same issue. I'm trying to add component to app named common. Here's how common/components.py looks like:

from django_components import component

@component.register('permission_button')
class PermissionButton(component.Component):
    def context(self, permission, url, caption):
        return {
            'permission': permission,
            'url': url,
            'caption': caption,
        }

    def template(self, context):
        return 'common/components/permission_button/permission_button.html'

I get TemplateDoesNotExist unless template itself is not in <BASE_DIR>/common/templates/common/components/permission_button/permission_button.html

from django-components.

EmilStenstrom avatar EmilStenstrom commented on May 29, 2024

@Real-Gecko Django only looks for templates in directories you have allowed in your TEMPLATES and STATICFILES_DIRS settings. So add your paths to your templates there.

from django-components.

Real-Gecko avatar Real-Gecko commented on May 29, 2024

Yep, but adding each component's template dir to settings.py does not look that exciting, and keeping templates separately is against component philosophy, I guess custom template loader for the app is the way to go.

from django-components.

EmilStenstrom avatar EmilStenstrom commented on May 29, 2024

@Real-Gecko Agreed. I'm opening this issue again, and changing the title to this. Would you mind making a PR?

from django-components.

Real-Gecko avatar Real-Gecko commented on May 29, 2024

Never done this before, will investigate Django documentation on custom template loaders.

from django-components.

Real-Gecko avatar Real-Gecko commented on May 29, 2024

It turned out to be pretty easy, check #85 for details.

from django-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.