Comments (8)
Good luck!
I just had a read through the discussion in #333.
From that discussion, it feels like you wish to achieve 3 things simultaneously:
- Define components in multiple locations
- Import components in
{% component %}
block by specifying the component path. - Have clear distinction between global and "local" components
As for the first point, I plan to do that in my project, but it's low in priority. But I think it should be possible with the STATICFILES_DIRS
as discussed above.
As for the second point, that's an interesting idea. Though I'd argue that using unique component names would be more maintainable approach. Because consider it in the context of e.g. VSCode. With their Python language support, when I move a python file, VSCode updates the import paths for me. But if you imported components via import path, and you moved the component file, you'd need to remember to fix it yourself.
As for third point, might be relevant to see #265? I haven't read that one though, so just guessing.
As for naming components, what I do in my project is that:
- UI components (my "globals") are unprefixed and named after the material design component names, e.g.
dialog
,button
, etc. - "Business logic" components (my "locals") are prefixed with group/module that describes it. E.g. if I had a shopping cart page, the page-specific components would be called
cart_list
cart_list_item
, etc.
from django-components.
Good to know that 0.50 is working. The changes surround the component imports were made in v0.60.
What's does your settings.py look like? Have a look at how the demo project is made https://github.com/EmilStenstrom/django-components/blob/master/sampleproject/sampleproject/settings.py#L130. There, in the settings, there's
STATICFILES_DIRS = [BASE_DIR / "components"]
This is what tells django-components where to look for components.
Try adding the directory where components.py
is to the STATICFILES_DIRS
.
from django-components.
I'm closing this thread as it seems there's no error here one django-components parts, just internal changes that broke a script.
from django-components.
Hey @TreyWW, the addition of _resolve_component_relative_files
was my doing, it was to enable us to refer to template and media files relatively to the component class.
My guess is that, based on your snippet, it seems like the error occurs because the component class is being created dynamically, and therefore __module__
is not populated.
Could you try manually adding __module__
to the third argument like below?
print(class_name, component_base_class, template_name)
component_class = type(
class_name,
(component_base_class,),
{"template_name": template_name, "__module__": "<python.path.to.file.py>"},
)
component_class = class_decorator(component_class)
Moreover, looking at the full script, it looks unconventional. Could you describe in more details what's the purpose of the script?
The reason is, if the only purpose of the script is to avoid the boilerplate code for the components, then in I think 0.32, the support for single-file components was added.
With single-file components, you don't need separate files for Python and HTML files, so that could be a good replacement for the your script. And that would also resolve your issue, as the classes would not be defined dynamically, so the __module__
value would be populated automatically with the python path to the file where the component is defined.
from django-components.
Hey @JuroOravec, thanks for the reply back.
Could you describe in more details what's the purpose of the script?
Basically I want to have components that are in files, so lets say modal.html
but prefixed, like +modal.html
to symbolise that it's a component. Because I never needed the feature of backend code per component it seemed an annoyance to manually make a class every time I add a component. So this script was aimed to just fill them in for me. Scan all my files, find ones with the prefix of + and add to components.
With single-file components, you don't need separate files for Python and HTML files, so that could be a good replacement for the your script.
But it still needs the python part right? I just want the HTML part.
I'll give the module approach an attempt now though, thanks! I'll let you know if it works.
from django-components.
🤦🏽 I can't reproduce this now. I've upgraded to 0.61 but now my components.py file isn't getting read at all which is odd because earlier it was (hence why I got the errors).
It goes straight to
Performing system checks...
[DEBUG] Template loader will search for valid template dirs from following options:
- /media/trey/projects/MyFinances/frontend/static
2024-03-29 21:25:00,967 django_components [DEBUG] Template loader will search for valid template dirs from following options:
- /media/trey/projects/MyFinances/frontend/static
[DEBUG] Template loader matched following template dirs:
- /media/trey/projects/MyFinances/frontend/static
2024-03-29 21:25:00,968 django_components [DEBUG] Template loader matched following template dirs:
- /media/trey/projects/MyFinances/frontend/static
System check identified no issues (0 silenced).
And then when I try and visit the site iget a "Component "x" is not registered". Not too sure why it's not reading my components.py file though
from django-components.
Aha, downgraded to 0.50 and now it's getting read again.. I'm going to take a look at the changelog to see if something was removed when reading components.py
Edit: Yeah i'm not too sure.. Updating to 0.50 seemed to at least work and all I had to do was change the template blocks from _block to just component and add endcomponent tags. So that's a start at least.
Though I agree, this script shouldn't be necessary, it kind of feels like it is unnecessary and I have no idea how it even works now so it's not really something i'd like to say is production ready lol.
This originally came from my discussion: #333
But i'd be happy to hear alternatives because I've kind of turned this into a huge mess
from django-components.
Ah yeah that's probably the issue, I removed components from staticfiles dirs for some reason. I'll give that another shot tomorrow then on the latest version, hopefully it works!
Thanks, your help has been much appreciated
from django-components.
Related Issues (20)
- Revert back to "django" as the default setting for "context_behavior"
- Refactor accessing of default slot from `as var` to kwarg HOT 3
- '
- Bug: Cannot override `block` tag inside `component` tag inside a template imported via `extends` in "django" mode HOT 2
- Support for dynamically loading component's JS/CSS dependencies HOT 2
- Typo Preventing html_attrs defaults from working HOT 2
- Inline tests/templates to make tests easier to follow HOT 1
- Allow to render a component, passing args and kwargs, from Python HOT 3
- Support for Paths as objects is missing HOT 25
- Allow users to customize component tags + reintroduce component "inline" block HOT 3
- This hyperlink is dead - There is no "Registering Components" in latest readme (0.82) HOT 2
- safer_staticfiles working? HOT 9
- Proposals to make it possible to keep HTML attributes inside the HTML file HOT 3
- problems with setting up django-components HOT 3
- Link to tutorial videos from readme? HOT 2
- Remove requirement for APP_DIRS=False and adding the cached template loader? HOT 1
- Auto-merge dependabot PRs? HOT 2
- Doc table of contents links not working
- JS and CSS not loading HOT 2
- Global context not available when rendering via view methods with RequestContext HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-components.