Using Snowpack to transpile Web Components and package dependencies - django

I am using Django to build a SSR website and using Django to serve static files. I also built some Web Components using lit-element and Typescript. I would like to avoid the complexity of Webpack and use Snowpack instead. Components are stored at /static/js/components.
To use the components, I need to (1) transpile them to Javascript, (2) make available their dependencies (e.g. lit-element) and (3) move the transpired files as well as the _snowpack folder to Django's /static/ folder.
Using just snowpack build does both but creates a build folder with a complete copy of the application. It is my understanding that buildOptions.out only moves the output folder (thereby overwriting static/ altogether if buildOptions.clean===true). I guess it would be possible to script the copying of the necessary files and delete the rest of the build folder immediately, but that strikes me as quite inelegant.
Is there a configuration to only create _snowpack and the transpiled files to a specific directory?

The Django staticfiles module supports having multiple directories for static files, and making them all available under the same /static/ URL.
That way, you won't have to copy those different type of assets into the same folder before being able to serve them with Django.

Related

How can I change the name(s) of folders in my Django project without destroying its ability to find its own parts?

I'm reading through Two Scoops of Django and the authors note that best practices involve having a config folder and an apps folder. I've been building a Django project for the last few months here & there and want to get in the habit of complying with these practices, but when I change the name of my <project_name> folder (with the wsgi, settings, etc.), Django can't seem to find the contents of the folder anymore.
How do I change this folder name and put the project apps into an app folder without breaking the connections they have?
Restoring connection can be a painful process and even if you restore the connections, there is no guarantee it will always work (eg, some 3rd party app may fail because of dependency issues which you forgot to change).
I do like to separate my created apps and project folder to be visibly different too. For this, I create parent folder which would be my entire django installation and then inside the created folder I create the project while telling that this is the directory I'd like to use. Lets say I want to create blog project:
$ mkdir blog
$ cd blog
$ django-admin startproject blog_project .
This will give you a blog folder and inside it you will get blog_project folder and beloved manage.py.

Mezzanine Static files not serving from STATIC_ROOT

I am new to Mezzanine and not an expert in Django. I am trying to make changes in the default Mezzanine theme. I am doing these steps to override default Mezzanine theme,
python manage.py collectstatic
A new static folder is created which stores all the static files across the project in this folder (STATIC_ROOT). This includes Mezzanine default static files as well from virtualenv/lib/python2.7/site-packages/mezzanine/core/static/
Then I run
python manage.py collecttemplates
A new templates folder is created which stores all the templates across the project in this folder.
Now when I make changes in files from /templates directory I get to see those changes in development server.
but when I make changes in /static directory files I do not see those changes in development server.
to make changes to css files I have to go to
virtualenv/lib/python2.7/site-packages/mezzanine/core/static/ path and then only I can see the changes in development server.
I want to know what am I doing wrong. I do not want to make changes in mezzanine css files, I want to overwrite them form static_root.
In mezzanine and in django sites in general, you should not edit files in the /static folder directly since these will be overwritten each time you use the collectstatic command.
Each of the templates Mezzanine provides can be found in the templates directory of each Django app that Mezzanine is comprised of. E.g the base template in mezzanine/core/templates/base.html.
To edit the templates you’d like to modify, copy them into your project’s templates directory and modify them there. You can also use the collecttemplates command to copy templates over automatically. Run python manage.py collecttemplates --help for more info.
The mezzanine template lookup system is described here. This post may also be useful
OKay, After some research I realized, This is not the way to overwrite templates. You want to copy static folder from Mezzanine package and paste it in an app where you want to use it, then edit that copied files. This is written in documentation for mezzanine, I missed that part.

Using GitHub Pages, is it possible to specify the root web directory when not using Jekyll?

I'm using another static site generator and I'd like to be able to check-in the source files (in markdown) as well as the generated site to my username.github.com repo. So, much like Jekyll but I'm not using Jekyll. My structure is:
--source
1.md
2.md
--target
index.html
1.html
2.html
--css
app.css
index.html (GitHub Pages wants the root here)
GitHub Pages is serving the index.html and expects the rest of the site to be at the root. I would like the site to be served from target/index.html. This would allow me to create a source/3.md, generate it locally, and push both source/3.md and target/3.html.
The only ways I can think to do this is to:
have 2 repos - one for the markdown and generate to another for just the html.
have a branch that contains the markdown and commit the source to that branch and switch to master before committing the generated target html.
keep the markdown in the root (instead of 'source') and generate the html to the same root directory
as a long shot, adopt the Jekyll diretory structure and hope it doesn't interfere somehow by thinking it's a true Jekyll-style site
reviewing the jekyll docs https://help.github.com/articles/using-jekyll-with-pages it seems that isn't possible. The source directory can't be changed.

Where is the appropriate place to put JavaScriptMVC files in Django project

Django 1.4 has updates its default project layout, so my new project layout becomes
root
manage.py
/project
/settings.py
/app1
/static
/app2
/static
Now I'd like to use JavascriptMVC framework for my project, where is the best place to place it? It's huge(39M) just for the bare framework.
I don't want to have a framework inside each of my app. So, is there a way to host it in a central place and used by all my apps?
I personally put my project-wide static files in the top level directory (root/static/) and add it to STATICFILES_DIRS
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-dirs
It makes much more sense to me to keep it in the same level as apps. I only put project configuration related files in my root/project/ equivalent folder like settings.py, urls.py, local_dev flags / local settings, etc.

How to tell Django staticfiles to not collect user uploaded media directory files?

I have a very simple question which I was not able to find an answer. Using Django staticfiles app, I call the statics collect routine by typing this command line : python manage.py collectstatic.
My problem is that I've several applications serving statics + a media directory containing user-uploaded files. Django copies all the files to the static directory, including media files!
I just would like Django to not copy user-uploaded files to the static directory when I call python manage.py collectstatic. Does anyone have an idea? Tried --ignore option but without success...
Thank you in advance.
That was a problem of django.contrib.staticfiles.finders.DefaultStorageFinder, try removing that from STATICFILES_FINDERS.
So what exactly doesn't work if you use --ignore?
Though the real solution is not to mix user generated content and static files.
If you keep both types separated you can easily put your static files under version control and don't have to deal with excludes on this front as well. And in fact the separation from media (user generated) and static files is the rationale behind staticfiles:
In previous versions of Django, it was common to place static assets
in MEDIA_ROOT along with user-uploaded files, and serve them both at
MEDIA_URL. Part of the purpose of introducing the staticfiles app is
to make it easier to keep static files separate from user-uploaded
files.
The docs on this topic are really comprehensive: https://docs.djangoproject.com/en/dev/howto/static-files/