meeb/django-distill

Configuring output of % static in django ?

Closed this issue · 1 comments

kc1 commented

enter image description here

I'm experimenting with django-distill (https://github.com/mgrp/django-distill) to generate a static site from a django project. i'm using django 1.10.8 . My main template contains:

<!-- Bootstrap Core CSS -->
{% load staticfiles %}
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet" />

<!-- Custom CSS -->
<link href="{% static "css/landing-page.css" %}" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.css"/>

The paths to the css and js files are correct when I run the project locally on my windows system using a dev server. However when I look at the source html I see:

enter image description here

on my windows system this works with a running sever, i.e.

 http://127.0.0.1:8000/static/css/bootstrap.min.css

but it breaks with static files and I have to change these to

  <link href="./static/css/bootstrap.min.css" rel="stylesheet" />

Is there any way to set % static to render to

./static/ instead of /static/
meeb commented

Are you opening the static files after generation locally just by opening the compiled static site output directory and clicking on files? Without a web server the /static/ path makes no sense given local HTML files have access to the file:/// local file system. If so that would break paths, yes. Distill doesn't alter the default behaviour of Django, if you open your site in a dev server on 127.0.0.1:8000 (which doesn't use Distill) the static files paths are the same.

If you want to generate pages you open locally from directories on your PC you could probably hack around with the STATIC_URL setting to be file:///some/local/path/ probably, but then these files would only work on your PC and a more sensible option would be to use Django as it is meant to be used to generate functioning websites and then run a dev server for it locally.

To test a generated site locally with working static media and paths you can just use the built-in Python web server by running python -m http.server with Python3 in the compiled static site directory (which is python -m SimpleHTTPServer in Python2). This will spawn a read-only web server on port 0.0.0.0:8000 and as a web server your static site will have correct paths again to test with until you upload it somewhere.

I'll close this as it's not an issue with Distill itself, but thanks for using the library.