No CSS and JS files when deploying with Heroku - django

I'm trying to deploy my project to heroku, but I faced a problem: there is no CSS and JS in deployed site. When I open page source of deployed site, for example here is next script in page source:
<script type="text/javascript"
src="/static/static/scripts/validation.js">
When I go to src link, it shows internal error. I guess that if my scripts were saved somewhere in Inet and src would be a link to it, then it would work correct. But when deploying my site, all my scripts and CSSare in static folder. When run site locally, it's okay, when through heroku, it can't see CSS and scripts. Is there any solution for it?

Instead of relative path linking use static template tag.
{% load staticfiles %}
<script type="text/javascript" src="{% static "somepath/validation.js" %} ">
set STATIC_ROOT and run python manage.py collectstatic
Check the Heroku doc for Django and Static Assets
Official documentation for Managing static files.

Related

How do I overwrite Bootstrap SCSS variables for my django website using PyCharm?

I'm using PyCharm to manage my django files and for the front end I'm using Bootstrap. I'm currently using the CDN method to point to the bootstrap files in my base.html file.
I've recently come to a point where I want to customize some of the SCSS variables that bootstrap provides, but I'm puzzled and can't find a guide anywhere to how to do this with my configuration.
I used npm to install the bootstrap files to my outer project folder. I've tried installing django-sass-compiler using pip, and then adding the required setting to my settings.py, but when I ran the command "python manage.py sass-compiler" (as per the PyPi docs), I got "ModuleNotFoundError: No module named 'django_sass_compiler'"
I'm assuming that if I were to get that working then it would compile my custom SCSS file into a CSS file that would overwrite my current 'main.css' (is that right?), however I can't even figure out that part.
If someone could please point me in the right direction then that would be great, there really isn't a lot of help out there for sass, pycharm, django & bootstrap together.
Yes, you will have to compile the CSS manually. You can use tools like Koala.
To accomplish that in Django, You will have to:
Create a directory named static at your project level directory. In satic add another folder called css. Go ahead and add the compiled CSS file.
Change
STATIC_URL = 'static/'
to
STATIC_URL = '/static/'
and add
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
In your base.html file at the very top add
{% load static %}
and you can link the base.html file to your compiled CSS file like shown below:
<link rel="stylesheet" href="/static/css/mycss.css">

Why we need `django-webpack-looader` to serve Vue SPA application through django?

I want to serve my Vue application (as SPA) through django, and I passed across this article. The author use django-webpack-loader to achieve this, but is not adding a simple TemplateView around dist/index.html will do the job:
url(r'^app/', TemplateView.as_view(template_name='index.html'), name='app'),
also this require altering settings.by as follow:
'DIRS': [
BASE_DIR + "/templates/",
BASE_DIR + "/dist/",
]
My question is what is the role of django-webpack-loader in the process if the app is already build using vue-cli?
After trying my approach without django-webpack-loader, I concluded that after each build, you have to do the following:
Put your entry-point (index.html), in a place that django can find it.
Put your assets (css, js, images) in the static folder.
Edit your index.html so it can correctly locate and load the scripts and css files, e.g:
<script src=/js/chunk-vendors.618e754e.js></script>
<script src=/js/app.bbfdaab6.js></script>
Should be:
<script src="{% static 'js/app.bbfdaab6.js' %}"></script>
<script src="{% static 'js/chunk-vendors.618e754e.js' %}"></script>
So django-webpack-loader should helps you with automating this task. without going through django staticfiles system.
Also I have read that django-webpack-loader can provide hot-code reloading during development but I did not try this out. Also I could not find information on how django-webpack-loader works really, so consider this answer incomplete.

Django how to use npm modules with static/ templates

I have recently added npm to my project in order to keep better track of my js dependencies. Previously I have just been git cloning to my static/vendor folder.
I've also added gulp , but have only got it doing hello world things right now. It seems simple enough - it can watch files, minify assets, compile Sass. Eventually I will probably switch to Webpack, but gulp is simple and working for now. I don't want to use django-compressor or django-pipeline.
So let's say I run npm install vis to pull in visjs. It gets added to node_modules and a record is added to package.json dependencies.
A) Do I reference vis/dist right where it is in node_mods in my template scripts?
<script src="/node_modules/vis/dist/min-vis.js' %}"></script>
# right now it's <script src="{% static 'vendor/min-vis.js' %}"></script
B) Should gulp be listening for changes to package.json dependencies and replicating vis/dist to static/vendor or static/js when it sees changes there?
I keep seeing people talking about handling STATICFILE_DIRS when they talk about npm and gulp. Right now mine is just set to the following. Will this change?
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_DIRS tells Django where to find all the static assets (which will later be "collected" for a deployment, but we can ignore that for the moment). That setting will need to include wherever you have gulp placing the processed assets, or you can have gulp put them into $BASE_DIR/static to avoid changing your setting if you prefer.
From there, you should be using the static templatetag
{% load static %}
<script src="{% static 'your_bundled_asset.min.js' %}"></script>
Note that if your asset is in a nested directory under the STATICFILES_DIRS entry it is under, you may need to prefix that. {% static 'js/asset.min.js' %}.
So Django can use the dev location ($BASE_DIR/static) or the "collected" location when deployed (./manage.py collectstatic --noinput pulls all static files to one location for simplified serving).

Heroku's Django is not loading lity.css and lity.js

I am trying to use this simple lightbox for embedding a video on my Django project. Locally with:
python3 manage.py runserver
it runs fine. However, once deployed to Heroku the app struggles to find the css and the js of lity, though it is located in the correct folder. Has anyone run into a similar problem?
The project is live here:
https://dry-depths-69493.herokuapp.com/
And the git-repo is here:
https://github.com/Datenrausch/heroku
Looking at your code in github, I see that ... master/honoradar/static/honoradar/lity/ has no subfolder named dist, but your HTML looks for that subfolder.
Try changing these lines from index.html to the correct files (I'm not sure which are the correct ones)
<script src="{% static 'honoradar/lity/dist/lity.js'%}"></script>
<link rel="stylesheet" href="static/honoradar/lity/dist/lity.css">
or add a dist subfolder with the correct files.

Django collectstatic command is not able to compile animate.css

I tried to use Animate.css in one of my Django Project (if interested here's the link).
Overview:
So to deploy the code, I run python manage.py collectstatic, as always. Till I don't include the animate.css in the {% styles css/base-min.css %} tag using django-sekizai and django-compressor together with the help of GIST.
The collectstatic just works fine, as soon as I include it in the project, the same command stucks.
For time being to use the animation.css in the project, I have directly included it with the help of
<link href="{{STATIC_URL}}/css/animate.min.css"
rel="stylesheet"
type="text/css"
media="screen"
/>
But, the question remains, why is the collectstatic commands fails in general to compile the animate.css?