Django/Vue -> Heroku: Static files not loading at production server - python

Static files load when DEBUG=True (locally and at the dev server), but not when DEBUG=False (production).
STATICFILES_DIRS is set to my dist dir created by Vue, and dist is not in the .gitignore. Heroku runs collectstatic on every deploy by default (I have not changed this).
The actual error is a 404 when trying to load any static file. The whitenoise package is being used. I've updated the middleware settings, and wsgi.py according to the docs, and have the settings variable which enables compression via whitenoise set (also according to the whitenoise docs).
whitenoise usually works fine with other apps. I'm not sure what's wrong with this. The difference is that I'm using Vue for the first time. I'd never used a js framework before.
When setting DEBUG=False locally, staticfiles still load fine, so I am unable to debug in that way.
Can anyone help?

django-heroku was the problem. In my settings I was using django-heroku. Removing it got staticfiles to load at production.

Related

how to fix django staticfiles page not found in heroku?

static files are not working in production in Heroku.but when in development it works, since yesterday I couldn't solve it. please help me.
settings.py
STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles')
STATIC_URL = '/static/'
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
CRISPY_TEMPLATE_PACK='uni_form'
LOGIN_URL='/login'
LOGIN_REDIRECT_URL='/app'
LOGOUT_REDIRECT='/'
During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
See Deploying static files for proper strategies to serve static files in production environments.
See Django documentation and Heroku documentation

Running WhiteNoise locally for Django development

I'm trying to serve static files on a Django project with DEBUG = False. I found the WhiteNoise project and configured it according to the docs, and my project works great when deployed to Amazon Web Services. However, when I try to run the project locally, the static files aren't served. I've tried running python3 manage.py runserver --nostatic and also adding 'whitenoise.runserver_nostatic' in INSTALLED_APPS per the docs here. The static files still aren't being served locally, though. Does anyone know if there are some other settings or configurations I should be changing?

Django shows 404 error on media files when debug = False in production

I'm trying to put a django website live and after going through the deployment checklist and turning debug = False django gives out a 404 error and only static files are loaded no media files uploaded from users are displayed. However with debug=True all the images and files work properly. below are the configurations of the project with debug = True. The console logs show multiple missing files.can someone point to me what i'm doing wrong?
settings.py
urls.py
console errors
Basically you are putting the application in DEBUG=False "mode", this means that django won't serve any static files. But on the other hand, if you want to test locally how DEBUG=False affects your environment, locally you can use python manage.py runserver --insecure option which will allow django to serve static files. But I must note that this is vastly inefficient and maybe insecure too.
Hope this helps.

Django static files 404 not found nginx

I am developing a Django application and I want to put it online on DigitalOcean so I can show it to a remote future user. I am quite new to django and python and I have never in my life touched the deployment of web applications, so I'm a bit lost in all that.
I used the one-click install of a django droplet in digitalocean, which uses nginx and gunicorn. I was able to make the app load in the browser using the ip address (159.203.58.210), but the problem is that no static files can be loaded.
I googled that for some days and nothing could help me. I'm guessing it has something to do with either permissions not allowed or that I did not write the static files correcly in my templates and *.py files.
I'm running my application with the command:
python manage.py runserver localhost:9000
I'm using sqlite3 as db
My complete code is here:
https://github.com/gbastien1/carte-interactive
On the server, my app is at /home/django/international
(where international is my site name, the app is called carte_interactive)
My staticfiles settings in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT= '/home/django/international/carte_interactive/static'
my staticfiles settings in /etc/nginx/sites-enabled/django.conf (same for media)
location /static {
alias /home/django/international/carte_interactive/static;
}
In my templates, I use {% load staticfiles %}
And I call my static files like so:
{% static 'carte_interactive/css/style.css' %}
And in my .py files, I sometimes call those files like this, which I don't know if it's right:
app_name='carte_interactive'
json_data_url = static('carte_interactive/json/data.json')
json_data_file = open(app_name + json_data_url, 'w') <-- here
It was the only solution that worked on development to access my static files in views.py, but it might be a problem in production with collectstatic and all, I don't know.
I am using django 1.9.4 on server and 1.9.1 on development it seems.
Do you have any idea why my staticfiles are 404 not found on the server but everything works fine on development?
Could there be a permission issue with the folder /home/django/... for the browser to reach my static files?
Is there a better way to call my staticfiles in views.py?
Thanks in advance!
edit:
The console error I get for all my template static files:
http://159.203.58.210/static/carte_interactive/css/style.css Failed to
load resource: the server responded with a status of 404 (Not Found)
I assume that you don't use nginx to serve static assets in
development? Runserver can serve static files, but very much slower
than nginx, which becomes a problem once you have more than a single
web site visitor at a time. You can remove the nginx alias for static
and reload nginx to let runserver serve the files, to confirm whether
it's a problem in the nginx config.
HÃ¥ken Lid
I removed nginx and made the Django server load the static files, and now I can show it to my future users. This answered my question, though it did not solve the problem itself! Thanks anyway !

How to serve static files in production for Django 1.3

I'm deploying my app and I wonder what I'm missing.
I did the following:
Set my STATIC_ROOT to an empty folder in my server.
Set the STATIC_URL to '/static/'
Added 'django.contrib.staticfiles' to INSTALLED_APPS
In development my static files are in the root of my app in a folder named static.
So, I ran manage.py collectstatic and all my files were copied to my static_root.
However, it doesn't work. I don't know if i'm missing any step.
Any help would be great
Thanks
. 4. Point Apache at your static folder.
As explained in Django's documentation, Django serves static files itself in development only, when deploying your application in production, it's up to you to make your web server (apache, lighttpd, nginx, whatever) serve the static files.
Django's documentation provides instructions for doing so with Apache here
Django while it's not in debug mode should not serve static files, for performances reasons, you should use:
./manage.py collectstatic
then configure your web server (apache or nginx) to serve this folder to the right url.

Categories

Resources