Django 1.8 Not Serving Static Files in Development - django

I am using the exact settings as in https://docs.djangoproject.com/en/dev/howto/static-files/ and my app is not loading the files. I've spent way to long trying to solve something so trivial but it's just not working. I've looked at alternatives, but these didn't work either.
This is in my setting file:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
This is at the end my urls file:
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I did a collect static when I had the static file dirs so there should be the correct files in my static dir. I also created test files and nothing.
Heres what it will say in the debug view:
Request Method: GET
Request URL: http://localhost:8000/static/assets/stylesheets/style.css
Raised by: django.views.static.serve
Last time I wrote an app this worked fine, i'm not sure why is isn't now.

You haven't used the exact settings in the docs...Which is fine, but you need to at least do the following, if DEBUG = False:
Set up STATICFILES_DIRS. This should be the absolute path to the folder where your static assets live (before they're "collected").
Set up STATIC_ROOT. This is different than the above path, and is where all of your static assets will be collected when you run collectstatic.
Run the collectstatic command to collect (i.e. copy) your assets from your STATICFILES_DIRS to your STATIC_ROOT
Depending on your configuration, and if DEBUG = True, you don't even need to define STATIC_ROOT in development mode.

Related

Why Django 4.0 can access static files only with STATICFILES_DIRS, not STATIC_ROOT

When I use STATIC_ROOT:
STATIC_ROOT = BASE_DIR / 'static'
Django cannot access static files and always return 404 error for them.
But when I use STATICFILES_DIRS:
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Everything works well. What's the issue?
I don't understand why Django works in this way, I always thought that STATIC_ROOT is correct way of creating route.
STATIC_ROOT is where the collectstatic command will copy all the static files in.
This folder is meant for production use. So that all the static files in your project (from your apps, or third party apps etc.) are kept in a single folder and therefore becomes easy to serve in production.
By default, Django automatically searches for static files inside each app directory (this is helpful in writing reusable apps). The STATICFILES_DIRS setting allows you to define extra places to look for the static files.
Also, the STATIC_ROOT folder and the STATICFILES_DIRS folder should not be the same values.

Using static folding inside app folding Django

I have a static directory inside my apps folder. I added this line of code to my projects settings.py file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
The file continues to result in a 404 not found. I'm getting stuck, what should I do?
It probably isn't mistake in your static files settings. But to be sure the relevant settings for static folder in app directories is STATICFILES_FINDERS https://docs.djangoproject.com/en/2.1/ref/settings/#staticfiles-finders. But the AppDirectoriesFinder should be present by default so it is probably OK.
Your problem is probably in your URL scheme:
For development purposes make sure you have django.contrib.staticfiles in your installed apps, DEBUG = True and STATIC_URL = '/static/' (or something similar). Then you should be able access the files.
For deploying it is more complicated but essentially you have setup STATIC_ROOT, before starting the server run python manage.py collectstatic command which will copy all static files to one location and then setup your server (Nginx) to serve the files from this folder under /static/ URL. Here is the official documentation https://docs.djangoproject.com/en/2.1/howto/static-files/#deployment
Note: if your app is foo and you have bar file in your static directory under foo app the URL for the file will be /static/foo/bar.

Django serve files from STATIC_ROOT and use AppDirectoriesFinder at same time

I'm trying and failing to figure out a config for serving static files that works for me under these requirements:
I want the collectstatic command to work always (when Debug=True, and Debug=False), and to collect app dir statics. This implies I need both 'django.contrib.staticfiles' in INSTALLED_APPS and 'django.contrib.staticfiles.finders.AppDirectoriesFinder' in STATICFILES_FINDERS.
I want to serve some files out of STATIC_ROOT with Debug=True (Debug=False I want to serve all files out of STATIC_ROOT), that don't belong to any particular app. I just do.
I want the dev server (./manage.py runserver) AppDirectoriesFinder lookups to work when Debug=True.
As a compromise I could live with all static files out of STATIC_ROOT, but I still need Debug=True and collectstatic to work.
But I can't figure out how to do this.
I can't add STATIC_ROOT to STATICFILES_DIRS (not allowed)
Adding urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to urls as suggested here does seem to not work in Debug=True with the dev server because the dev server overrides it and uses static finders.
Any ideas?

Setting up django static directory for testing/development

In production, the apache server can handle serving static files just fine. For testing on my local machine I'm using manage.py runserver.
It seems the django apps I have somehow inject some default search paths into this debug server. The browser url shows everything under /static/whatever, but those files are coming from all over the place. This is fine for testing but when moving to production I'll need them in the one path. Thankfully, manage.py collectstatic provides a handy method to copy everything to the one directory given by STATIC_ROOT in settings.py. I've set mine to os.path.join(BASE_DIR, 'static/') where BASE_DIR is where manage.py is.
Now I want to start adding my own stuff in static, but everything's still being hosted by these hidden search directories provided by apps under DEBUG=True. I've tried the following in urls.py:
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
and
import re
from django.conf import settings
if settings.DEBUG:
static_url = re.escape(settings.STATIC_URL.lstrip('/'))
urlpatterns += patterns('',
(r'^%s(?P<path>.*)$' % static_url, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
Neither have worked.
I tried providing my static directory in STATICFILES_DIRS, but get the error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. So then I tried removing my STATIC_ROOT so the path was only given in STATICFILES_DIRS. Finally I can access my files in development (still with staticfiles_urlpatterns from above).
However, I'm pretty sure removing STATIC_ROOT is not the right way to go about it. For a start, I won't be able to run collectstatic after installing other apps without manually changing everything around again. How should I be configuring my static directory for development?
Also, is there a way to turn off hosting app specific static directories so I can make sure my single static is set up correctly (but still be in development mode)?
You're making this more complex than it needs to be. Include "static" in STATICFILES_DIRS as you are doing, for the project-level assets, but make STATIC_ROOT - ie the destination of the collectstatic command - something else, probably "staticfiles", and alias that directory to /static/ in Apache. Django will serve your assets from STATICFILES_DIRS automatically, without you even needing to add the urlpatterns.
OK, I completely missed the point here.
STATIC_ROOT is for collectstatic to pool all the static content for you. You don't add your files to here because it gets overwritten.
Now for production you can get your webserver to host the directory STATIC_ROOT and point STATIC_URL to the url it can be found through the webserver.
STATICFILES_DIRS allows you to add your own directories for collectstatic to search and pool to STATIC_ROOT. Then you can nicely separate static stuff for each of your apps and/or a common project static.
For dev, django expects everything to be in STATICFILES_DIRS and hosts that, not STATIC_ROOT because then there would be duplicates. This makes sense for larger projects because you want to be working on the per-app files, not some blob of stuff for a website.

Django on Heroku -- static files not being found

I am running Django on Heroku. I can successfully run collectstatic, but when I go to the site it is obvious that Django is unable to find my static files. Here's a snippet from my settings -- I think it's mostly standard stuff:
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_DIR, 'static'),
)
# List of finder classes that know how to find static files in
# various locations.
if CLAYS_ENV == 'dev':
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
And in my case the CLAYS_ENV variable will be set to 'dev'. Any ideas on why Django can successfully run collectstatic, but then can't find the files afterwards?
Collect static and serving static media are two different things. Collectstatic just puts your static media in your STATIC_DIRS
Serving your static files is a process of the server not the collectstatic command. Ive never deployed to heroku before but do you have a url to map your static assets to their location?
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
Which server are you using? Usually you want the server to serve static media, though, instead of django. In apache this is done by setting up an Alias
It's recommended to serve static files through a CDN (like Amazon S3) when using Heroku. While you can still manage to serve those directly from Heroku, the same process attending dynamic requests is also serving static data, wasting processing time. Also, in case of media files, the use of a CDN is mandatory, since Heroku's file-system is "ephimeral": Every time you deploy new code, a new image of the Cedar stack is recreated from scratch and a new code checkout is made. Every file not Git-tracked created between deploys is lost.