Django 1.8 serving static files during deployment - python

My project runs fine on the local sever but during deployment on Heroku, I get the following error
Collectstatic configuration error. To debug, run: $ heroku run python manage.py collectstatic --noinput
I understand this is due to STATIC_ROOT settings in settings.py, but I can't seem to point to the location of the static files properly.
This is my folder structure.
C:/
/Users
/Mac
/denv
/musicpro
/musicpro
/featureapp
/static
/featureapp
/css
/img
/js
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'musicpro/featureapp/static/featureapp/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static")
)

Related

Django deployment not loading static files in Google app engine

Hi I am trying to deploy django app on Google app engine. My Django app works fine in the locally but in google app engine it is not working. I checked and found that issue is with my static files. My static files are not getting loaded in app engine.
********.appspot.com/static/youtube/about.css
Not Found
The requested resource was not found on this server.
Its been two days I have trying to follow answers on various forums but none worked for me. I have following in my settings.py
my settings.py code snapshot
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'youtube',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sitemaps',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Just for your information, I tried changing STATIC_ROOT to 'static' but didn't work.
STATIC_ROOT = 'static'
STATIC_URL = '/static/'
My directory structure:
my project dir structure
My HTML file from app youtube, and app directory structure
about page html file snapshot
<link rel="stylesheet" type="text/css" href="{% static 'youtube/about.css' %}">
My App.yaml
runtime: python
env: flex
runtime_config:
python_version: 3.7
entrypoint: gunicorn -b :$PORT yt_analytics.wsgi
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
I have run below command to test on local server and then deploy on Google app engine.
python manage.py runserver
python manage.py collectstatic
gcloud app deploy
When I run collectstatic command I see it is creating new folder named static inside my project root directory.. i.e. yt_analytics/static
Now I see I have two static directories in my project--
yt_analytics/static
yt_analytics/youtube/static
When I ran findstatic command I got:
python manage.py findstatic youtube/about.css
Output:
Found 'youtube/about.css' here:
/home/work/Mirror/Projects/django/ytwebsite/yt_analytics/youtube/static/youtube/about.css
Have exhausted all forums and answers before asking this question. I am unable to deploy my app on GAE. Your help is appreciated.
Adding below code in urls.py after #Ivan Starostin's comment and it worked for me.
from django.conf.urls import url, include
from django.views.static import serve
urlpatterns = [
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Thanks Ivan for your valuable comment.

Static files not loading on django localhost (404 error) while loading okay on heroku

Problem
I'm building a django web app. I can't get to display a static file (icon) in an html template when I run my app locally (ubuntu 20.04, python 3.8.5, django 3.1.4).
I DO NOT HAVE THIS ISSUE WHEN DEPLOYING ON HEROKU CLOUD, only on my localhost.
When loading up the webpabe on my localhost, I can see "GET /static/icons/graph-up.svg HTTP/1.1" 404 1781 in the Django wsgi console.
My project tree:
django-project
|_ ...
|_ static
|_ icons
|_ graph-up.svg
index.html
{% load static %}
<img src="{% static 'icons/graph-up.svg' %}" />
settings.py
[...]
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# I've tried with and without whitenoise, doesnt seem to change anything
MIDDLEWARE = [
# 'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
[...]
urls.py
urlpatterns = [
path('', views.home, name='index'),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I've followed the instructions from https://docs.djangoproject.com/en/3.1/howto/static-files/
Any ideas or suggestions welcome!
What I've tried locally so far
Without success (icon gets 404 error)
python manage.py runserver (to run with the wsgi from django)
gunicorn my_app.wsgi
heroku local (to simulate the heroku web server which is gunicorn locally)
python manage.py runserver --insecure (from Django Static files 404)
load the app locally with/without the whitenoise.middleware.WhiteNoiseMiddleware line in MIDDLEWARE after django.middleware.security.SecurityMiddleware
With partial success
Following an answer from Django Static files 404:
I replaced:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
with:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join('static'), )
I reloaded the wsgi and voila. The icon shows up.
However when I revert back to the original
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Just to see what happens, the icon still shows up.
Conclusion
I'm not sure this is the right thing to do but it seems to be working fine. Here's how I workaround the issue.
In my settings.py (for heroku):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
In my settings_dev.py (for localhost):
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join('static'), )
I don't know exactly what the problem is but to fix it you can use Whitenoise module, used to serve staticfiles
for Reference: https://youtu.be/kBwhtEIXGII
for in detail info.: http://whitenoise.evans.io/en/stable/

Django Collectstatic in Amazon Elastic Beanstalk Not Working

I have deployed my Django app in amazon elastic beanstalk. However, the css in the django admin is not working. I understand that it's a collectstatic issue. This is what my settings.py looks like:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
And I have this line in my ebconfig.config file:
container_commands:
01_collectstatic:
command: "python manage.py collectstatic --noinput"
However, this is how it looks like once I deploy it in amazon:
I have tried seeing the log files in amazon console. I found a line that reads:
EmbeddedPostBuild/postbuild_0_dcape/Command 01_collectstatic] : Starting activity...
.....
0 static files copied to '/opt/python/bundle/33/app/static', 102 unmodified.
What do I need to do to make sure it works?
I was having the same issue for the longest time. It seems like you have to tell beanstalk where your static folder will be located on the production server.
In your ebconfig.config where you specify commands/container_commands add an option settings so that beanstalk can know what your static_root equates to on the live server.
This is how I do it:
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
config file
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: MDGOnline.settings
"aws:elasticbeanstalk:container:python:staticfiles":
"static/": "www/static/"
Check out this wonderful AWS Elastic Beanstalk Doc for more detailed info

strange django static file behaviour

I'm getting 404 for static files when DEBUG=False in my settings. The file is served fine when DEBUG=True.
I'm looking at the docs and all my configs seem OK to me.
# my_settings.py
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/`
then I run collectstatic --settings=my_settings, and ls static to confirm it has collected the files. It has:
ls ls static/css/core.css
# (its there).
But when we request "localhost:8000/static/css/core.css" we get 404.
Note running python manage.py findstatic css/core.css --settings=my_settings fails to find the file. When I do DEBUG=True and run findstatic it finds the file, but in the sub app's static dir, not the collected directory (STATIC_ROOT).
Note I have:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
and "staticfiles" is in my INSTALLED_APPS.
Django 1.6
As you have specified DEBUG = FALSE in settings, its now left on HTTP server like nginx or apache to serve the static files. You need to configure your webserver to serve static files. An example for apache can be found here.

Django with ./manager runserver works well, while Django + uwsgi return 404

This si my settings.py about static in settings.py:
STATIC_ROOT = '/home/coat/www/site/app/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/usr/lib/python2.6/site-packages/django/contrib/admin/static/",
# This is Django admin default static files
)
I user django server:
./manager runserver
Then I open the URL: http://localhost:8000/static/admin/css/base.css
It works very well.
But a open http://localhost/static/admin/css/base.css
It print '404'
I had restart Nginx and uwsgi for many times, but it dosen't works.
First things first, this is nonono:
STATIC_ROOT = '/home/coat/www/site/app/static/'
Never hardcode absolute paths, you're just making your settings file less portable and probably killing kittens. Adapt this to your needs:
import os.path
import posixpath
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# fix STATICFILES_DIRS too
Now, to your question. django.contrib.staticfiles is fantastic but probably a little confusing at first.
You must understand the collectstatic command:
Collects the static files into STATIC_ROOT. [...] Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
With runserver, staticfiles are served automatically, but in production mode (DEBUG=False, real HTTP server like Nginx), you should run collectstatic to (re)build STATIC_ROOT
STATIC_ROOT: is the root path where the HTTP server should serve static files from.
STATIC_URL: is the root URL where the HTTP server should serve static files to.
STATICFILES_DIRS: other static directories, in addition to each app's "static" subdirectory. Because django.contrib.admin is a normal app with a "static" folder, there is no need to specify it in the settings.
Conclusion: if STATIC_ROOT resolves to /home/coat/www/site/app/static/, and STATIC_URL is /static/, then you should:
Run collectstatic management command
Configure Nginx to serve /home/coat/www/site/app/static/ on /static/, ie.:
location ^~ /static/ {
alias /home/coat/www/site/app/static/;
}
Reload nginx
In development the runserver command does some magic to serve your static files. In production you need to configure NGinx to do it. There are two chapters in the Django documentation I recommend you read:
Serving static files in production
Serving files (Deployment)
Especially the last link explains how you have to configure your NGinx to serve your static files.

Categories

Resources