meeb / django-distill

Minimal configuration static site generator for Django
MIT License
441 stars 35 forks source link

How to deal with deployments to not /? #81

Closed gregsadetsky closed 1 year ago

gregsadetsky commented 1 year ago

Thanks for the great package!

I'm currently deploying my static, django-distill powered site using github pages

The deployment url looks like this:

https://gregsadetsky.github.io/some-repo-name/

Because of this (i.e. not deploying to / like you'd typically do), things get... weird/complicated :-) I've done tests with STATIC_URL, tried to add prefixes in urls.py and even manually in my template html files. Even did a bit of terrible bash scripting to move stuff around in the generated directory.

Nothing feels natural or good. What would you suggest as the correct way of making this work? I'd want template <a href="{% url ... %}">...</a> links inside of my templates to work, and for images included using <img src="{% static ... %}"> to work as well.

Thank you!

meeb commented 1 year ago

If I understand your issue correctly this is more of a Django question than distill question. Specifically, how to run Django in a "subfolder" (or with a global URI prefix). Given distill just does whatever Django is doing, the issue really is to make Django work with the root of the project at /some-repo-name/, then distill it.

The two most common ways are tweaking settings.FORCE_SCRIPT_NAME:

https://docs.djangoproject.com/en/4.2/ref/settings/#force-script-name

or by using a global URL prefix:

urlpatterns = [path('some-repo-name/', include(urlpatterns))]

You'll probably still have to edit STATIC_URL as well. You can test all of this in the Django development server (runserver) before distilling the site.

If you want the super hacky way, you can could try using entirely relative asset paths rather than absolute paths and set a <base> HTML tag in your template, but generally I'd go with FORCE_SCRIPT_NAME if it was me.

gregsadetsky commented 1 year ago

Thank you!

I was a bit blanking yesterday. Here's how I made it work:

Did I make it a lot more complicated than it needs to be? :-) I mean in the end it's just 2 bash lines. It still feels a bit weird to do that?

meeb commented 1 year ago

That doesn't sound too weird. I've not used GitHub pages myself, so if it has that restriction and it's not editable then I would assume you'd need to tweak the path post-rendering. If it helps, Cloudflare pages works very nicely with django-distill and has a free tier...