Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to generate a site using the given assets directory leads to "'project' is undefined error" #1858

Closed
uwagura opened this issue Aug 6, 2024 · 8 comments
Labels
bug Something isn't working
Milestone

Comments

@uwagura
Copy link

uwagura commented Aug 6, 2024

This might be a user error and not a betty issue, so I apologize in advance if I misunderstood how the generate command works.

In order to create a site using the same templates as the demo site, I copied the betty/assets directory within this repo to the same directory as my betty.yaml file and ran betty -c betty.yaml generate. This caused the following error (I've removed the full path to most of these files):

←[91m'project' is undefined
Traceback (most recent call last):
Lib\site-packages\betty\gui\project.py", line 831, in _generate
    await generate.generate(app)
Lib\site-packages\betty\generate.py", line 114, in generate
    await _generate_static_public(app, job_context)
Lib\site-packages\betty\generate.py", line 285, in _generate_static_public
    await app.renderer.render_file(
Lib\site-packages\betty\render.py", line 74, in render_file
    await renderer.render_file(
Lib\site-packages\betty\jinja2\__init__.py", line 400, in render_file
    rendered = await self._environment.from_string(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Lib\site-packages\jinja2\environment.py", line 1327, in render_async
    return self.environment.handle_exception()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Lib\site-packages\jinja2\environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 4, in top-level template code
Lib\site-packages\jinja2\environment.py", line 487, in getattr
    return getattr(obj, attribute)
           ^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'project' is undefined←[0m

I got the same error when I tried to generate a static site using betty gui instead, and in both instances I did not modify any of the files in the assets directory. Let me know if I'm missing something, or if there is another issue at play here!

@bartfeenstra bartfeenstra added this to the 0.3.x milestone Aug 6, 2024
@bartfeenstra bartfeenstra added the bug Something isn't working label Aug 6, 2024
@bartfeenstra
Copy link
Owner

bartfeenstra commented Aug 6, 2024

Looking at the traceback this is a problem with a Jinja2 template file in a static assets directory that accesses a non-existent project variable. I did a quick check in Betty's templates and nothing's changed there in some months. With assets, the different sources are overlaid so you will never have to copy an asset from one source into another unless you intend to make changes. Did you customize any files?

Any such uncaught exceptions are a problem with the code, and never the user's fault, so thank you for reporting this :)

@bartfeenstra
Copy link
Owner

You may have noticed the traceback does not tell us which file specifically caused this problem. Instead, it just says <template>. I opened pallets/jinja#2013 to lay the groundwork for us to be able to set the correct filenames for these tracebacks in Betty.

@bartfeenstra
Copy link
Owner

bartfeenstra commented Aug 6, 2024

Now that #1862 is merged, would you be comfortable installing Betty from source (instructions) and checking if Betty now shows a traceback with a *.j2 filename in it, instead of <template>?

@uwagura
Copy link
Author

uwagura commented Aug 6, 2024

I didn't change any of the files in the assets directory, I just copied them over to the same directory as my betty.yaml file and ran the command above

With assets, the different sources are overlaid so you will never have to copy an asset from one source into another unless you intend to make changes.

Does this mean that the command line version of betty has access to these templates without having to copy them over to the directory you run the generate command in? Because when I initially ran betty -c betty.yaml generate it produced a site that seemed to lack a lot of the structure that's in the templates, which is why I copied them over in the first place

I can try installing from source later this week!

@bartfeenstra
Copy link
Owner

You will never have to copy existing assets, period. Does your project use Cotton Candy (the default theme) or a custom theme? Without a theme extension you will see very bare pages indeed.

@uwagura
Copy link
Author

uwagura commented Aug 7, 2024

It wasn't using a theme extension. Everything is working as expected with the extension added, thanks!

@bartfeenstra
Copy link
Owner

Excellent! I had already made a note some time ago to show a warning when no theme is enabled but not got around to it. There are valid reasons not to have a theme (testing, it's way faster not to use a theme if you're testing something that does not depend on front-end code) but the lack of any sort of warning should not make this the frustration experience you've had.

@bartfeenstra
Copy link
Owner

I opened #1867 to add a warning, although this will (initially) be for the upcoming Betty 0.4 version only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants