meeb/django-distill

Is it possible to customize rendering or add functionalities to existing rendering by getting pre/ post signals

Closed this issue · 8 comments

Hi ,

I find the library easy to use and simple to understand. I have certain requirements and would be great if i can solve it with the django-distill library.
Some information on the usecases.

  • Currently during the rendering process, the settings.STATIC_URL is used during generation process. Is it possible to change during the rendering process, that it points to the settings.DISTILL_DIR/static ?... currently the complete folder structure is generated and it works as long as i am opening the project in the same OS env(Ubuntu) but doesnt work when i change to windows for e.g.,

  • Currently media files used in the web project, are hosted in azure web services. I would like to have a pre signal so that i can also deploy the media files inside the settings.DISTILL_DIR. This will help in using the static files in an environment which is not connected to the internet.
    Thanks in advance

BR
SK

meeb commented

Hi,

I'm not entirely sure I understand your first question. Usually you would just run distill-local or distill-publish on whatever host you check your code out to, also I'm not sure I see why any statically generated templates wouldn't work on Windows if they were generated in Ubuntu.

As for your second question, do you want to publish your statically generated templates and media to an Azure object storage bucket? What would a pre-signal in this context do to be useful?

Cheers.

Hi,

My goal is to build an industrial machine documentation website which is accessible online and offline. The machine once delivered does not have access to internet(or is very restrictive), meaning the static and media files have to be present in the machine locally.

I have a static root hosted in azure for the online site. For the offline site, when I start the rendering, I want the htmls to point to the new deployment site(e.g., local folder). Below a bare minimum of my base html used for rendering:

{% load cdnstaticfiles %}

<html>
<head>

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" type="text/css" href="{% cdnstatic 'WebHelp/styles.css' %}">
    {% block title %}
    <title>WebHelp Platform </title>
    {% endblock %}
    <link rel="icon"href="{% cdnstatic 'favicon.ico' %}">
</head>
<body>
    <header class="site-header">
      <nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
        <div class="container">
        {% block navbar %}
        {% endblock %}
        </div>
      </nav>
    </header>
    <main role="main" class="container">
      <div class="row">
        <div class="col-md-8">
          {% block content %}
          {% endblock %}
        </div>
         <div class="col-md-4">
          {% block content-right %}
          {% endblock %}
        </div>
    </main>   
    <!-- Optional JavaScript -->
  </body>
</html>

I was expecting to use the cdnstatic tag to point to the deployment location, but in rendering it is still pointing to the original azure static.

For:

I'm not sure I see why any statically generated templates wouldn't work on Windows if they were generated in Ubuntu.

This had something to do with relative navigation to underlying htmls. I will prepare a snippet for this if interested.

As for your second question, do you want to publish your statically generated templates and media to an Azure object storage bucket? What would a pre-signal in this context do to be useful?

I was thinking of a pre-signal here to make a switch between which static storage location is used. The azure location for online page{% azurestatic url %} and the local location {% localstatic url %} for the offline page. This is context with using the distill-local deployment from bash

I am still a beginner to django so please excuse me if I have overlooked something, I hope I have been able to give you some context.

BR

meeb commented

Ah right, that's clearer, thanks. You might not have noticed what Django staticfiles does. When you do:

{% static 'some.file' %}

it just merges some.file with whatever you have set in your settings.STATIC_URL as per:

https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-STATIC_URL

If you're also using my cachebuster Django library it does exactly the same thing, just appends a ?build-tag to the URLs to provide cache-busting on CDNs. So I guess you have your settings.STATIC_URL set to https://some.azure.bucket/ or something which is causing the issue where your static URLs are pointing to the absolute URL of the Azure bucket?

Could you set settings.STATIC_URL to /static/ which will work to build the local site and static assets will have local URLs, then just copy it up to the Azure bucket?

If I find the time I'll look to see if I can add an Azure backend adapter, then you could just do something like:

./manage.py distill-local && ./manage.py distill-publish and have both the local and Azure-hosted sites built if that would be useful?

Hi,
I tried as you suggested. Now the static files copied and the references make more sense. I still have the following problem where the base html is rendered accurately from the css but the htmls inside a folder are having an issue with the referenced static files. I have tried to represent it in a diagram.
image

if there is not a possibility, i will try to manually add the relative path decorator based on the location of the html.

./manage.py distill-local && ./manage.py distill-publish and have both the local and Azure-hosted sites built if that would be useful?

Yes, then i do not have to locally copy all the static files from azure :)

meeb commented

Ah right, so you're actually opening the local files directly in a browser so you need static media paths like ../../static/css/file.css to work for sub-directories? I'm not sure Django supports this out of the box as it would require a different STATIC_URL per page generation based on the URL of the page. Almost all websites are served from a web server so having a path of /static/css/file.css works fine as it's absolute from the URL base not relative to the local filesystem.

You could just use /static/... absolute URLs and use a basic web server like python3 -m http.server of course... but I imagine that's not a particularly helpful suggestion.

The static path issue isn't a django-distill issue, that's a general Django usage question so you're probably better off searching for a solution to that outside of this issue.

I'll look into an Azure object store back-end when I get time though!

thanks a lot ... I have solved it by overriding static url in the individual pages. Thanks a lot for your swift response !

cheers !

meeb commented

No problem, glad you solved your issue. I've created an issue to track adding an Azure backend if you want to follow its progress in #48

meeb commented

The newly released 2.9.0 version contains an Azure blob storage backend. See the README for publishing target details.