I finished a Dokku deployment on a Digitalocean server. My application seems to work, except for my static files.
Here are the relevant parts of my settings.py file:
PROJECT_DIR = Path(__file__).absolute().ancestor(2)
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_DIR.child('static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = PROJECT_DIR.child('staticroot')
Does someone know how can I point my app at my static files on a Dokku based system?
Though it's not answering my question, we're currently serving the static files from AWS S3, and that works. It makes my question less urgent, but I still would like to know if it is possible to solve it "locally".
You may need to do a few things
run ./manage.py collectstatic to move static files to your static folder
Update your web server config so that /static/ points to the static folder.
Have you done this already?
Related
I have django application and nginx, from browser, it access to the file /static.
such as
http://example.com/static/file.png
However my application has files under /staticroot.
http://example.com/staticroot/file.png
it shows the image.
So, I set on nginx.
location /static {
alias /staticroot;
}
However it doesn't appear
My environment is Debug mode.
My settings py is like this,
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(config("PROJ_PATH"), 'staticroot')
How can I fix this?
in /etc/nginx/sites-available/you_project
set
location /static/ {
root /home/your_admin_name/your_project_folder_name;
}
in settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
In order to serve staticroot content you've to rename your STATIC_URL='/staticroot/' & in nginx file make changes like this
location /staticroot/ {
alias /home/your_username/project_name;
}
When request comes to /staticroot/somefile Nginx will look for directory named as staticroot in file system and it will serve provided file. So when you define url for your static or media content make sure you've created directory with the same name.
For more info check Serving Static Content on Nginx docs.
This Django app is deployed on Azure as an App Service its static and media files are stored in an Azure storage account - blob.
The project used to work well in the past, but something has changed and now the problem is as following.
Relevant part of the app settings file:
STATIC_URL = 'https://myappstorage.blob.core.windows.net/static/'
MEDIA_URL = 'https://myappstorage.blob.core.windows.net/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# any static paths you want to publish
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
so one would expect that for example a favicon, which is in the root directory of the storage could be found on https://myappstorage.blob.core.windows.net/static/favicon and it is indeed!
But all static files that the app on azure tries to load it tries to load from
https://myappstorage.myappstorage.blob.core.windows.net/static/
(note the duplication of myappstorage), same for media files.
This results in no static files being applied to the page as they are being loaded from the wrong url. When I run the app locally, it works fine. I destroyed it and recreated it, no success. Now I have two copies running, one deployed through FTP and startup command and on using Github action. Still the same problem.
I have also tried little hack-ish workarounds in the setting file, with no success.
Try with adding below in app setting
DEFAULT_FILE_STORAGE = 'backend.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage'
STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"
AZURE_ACCOUNT_NAME = "djangoaccountstorage"
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION }/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}
refer this document for more details
I'm working in a development environment with debug = True on Windows 10.
I have installed node and npm and run npm microm from the main folder of my project. It created a node_modules subfolder with a microm folder inside, among others. so the microm.js script is located in:
f:\roomtemp\node_modules\microm\microm.js
The relevant portion of my Settings file is:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'node_modules')
My template contains the following:
{% load staticfiles %}
<script src='{% static "/microm/microm.js" %}'></script>
When I attempt to load the page, the Chrome console shows a 404 and I see this in the server console:
"GET /static/microm/microm.js HTTP/1.1" 404 1661
I have read many SO questions that seem to contain the solution but none have worked so far. I've also searched high and wide on the internet. No luck so far. Any help would be much appreciated.
Mike
First of all I don't think you should STATIC_ROOT at your node_modules folder. STATIC_ROOT is where the django management commant collectstatic will put all the static files from all the apps into when you are readying for deploying. This process may result in some files being overwritten.
Please restore STATIC_URL to some sane value like
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
Then use the STATICFILES_DIRS setting. This is what tells django what extra locations to look for static files.
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'node_modules')]
Finally, make sure that your STATICFILES_FINDERS has these
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
For some reason, the static files which are to be loaded by djangobower.finders.BowerFinder are not loading (getting a 404 Not Found in the server)
settings.py
STATIC_ROOT = "/root/Desktop/django-DefectDojo/static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
)
BOWER_COMPONENTS_ROOT = '/root/Desktop/django-DefectDojo/components/'
BOWER_INSTALLED_APPS = (
'jquery-ui',
)
INSTALLED_APPS = (
'djangobower',
)
template
<script src="{% static "jquery-ui/jquery-ui.min.js" %}"></script>
project structure
-project root
-static
-components
-vendor
-assets
-bower-components
-jquery-ui
-jquery-ui.min.js
I do a ./manage.py bower install followed by a ./manage.py collectstatic
Now, on running the server, I get a Not Found.
However, when I make STATICFILES_DIRS = ('/root/Desktop/django-DefectDojo/components/vendor/assets/bower_components/',) then the static files get loaded.
But this shouldn't be the case as BowerFinder is supposed to be doing this.
Seems that this is not a one-off instance. This occurs when django-bower's finders.py is unable to locate either of the bower_components or the components directories in the provided BOWER_COMPONENTS_ROOT variable - which is what it looks for.
It is unable to do so because bower install now creates the bower_components directory as follows:
-projectroot
-components
-vendors
-assets
-bower_components
-
-
as against:
-projectroot
-components
-bower_components
-
-
The easiest way to resolve this is to set BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_ROOT, 'components\vendors\assets') as opposed to doing just BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_ROOT, 'components')
Related post from django-bower's repo:
https://github.com/nvbn/django-bower/issues/20
Bower created the bower_components directly in my Django root directory.
Therefore I just had to do this in the settings.py:
BOWER_COMPONENTS_ROOT = BASE_DIR
I have a project that has been running just fine for about 6 months. Static files have been working perfectly, and everything is great. I have my static files located in a folder as so:
/var/www/html/static/
In my settings.py file, I have the static section setup like so:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/html/static/',
)
This has been working just fine.
However, I now want to move the static folder to a different location. Specifically, I want to move it inside the main project directory. My project is located at /var/www/html/shq/ so I want to have my static directory located at /var/www/html/shq/static/. I moved the folder, then updated my settings.py file to look like this:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/html/shq/static/',
)
However, it didn't work. The Django project is still referencing the old location.
What am I missing here? Why isn't the Django project using the new location of /var/www/html/shq/static/?
EDIT
This is what the tail end of my settings.py file looks like:
119 STATICFILES_FINDERS = [
120 'django.contrib.staticfiles.finders.FileSystemFinder',
121 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
122 ]
123
124 STATIC_URL = '/static/'
125 STATIC_ROOT = '/var/www/html/collected_static/'
126 MEDIA_URL = '/media/'
127 MEDIA_ROOT = '/var/www/html/shq/media/'
128 STATICFILES_DIRS = [
129 os.path.join(BASE_DIR, "static"),
130 '/var/www/html/shq/static/',
131 ]
You might try doing something like this. I think it returns the list of directories that django looks for to find static files. Might help debugging.
from django.contrib.staticfiles import finders
from pprint import pprint
pprint(finders.find("", all=True))
Also, I may not be fully understanding your scenario, but you might confirm that the STATIC_ROOT is set to the location where you want to serve your static files (where your webserver will serve the files). The STATIC_DIRS setting tells collectstatic where to find static files, but the STATIC_ROOT is where collectstatic will actually place the files.
I figured it out. Not surprisingly, it was an easy fix once I figured it out.
It had nothing to do with my Django settings and everything to do with Apache.
Original
Alias /static/ /var/www/html/static/
So no matter what I did in my Django settings.py file, Apache was overriding that to send /static/ requested to the wrong directory.
New Apache Setting
Alias /static/ /var/www/html/shq/static/
Now the proper static files are being referenced. Hopefully this helps someone else in the future :)