0

I have a Django (3.1) app that I've set up to serve static files, that is, myapp/settings.py has

DEBUG = True

.
.
.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    .
    .
    .
]

.
.
.

STATIC_URL = '/static/'

STATIC_ROOT = 'static/'

But instead of serving files from the static directory of myapp, it's serving static files from /Library/Python/3.7/site-packages/django/contrib/admin/static/. I have tested this by adding files to both directories,

~/Projects/myapp/static/file-from-project.js
/Library/Python/3.7/site-packages/django/contrib/admin/static/file-from-library.js

and then

python3 manage.py runserver

with a template including the lines

<script src="{% static 'file-from-project.js' %}"></script>
<script src="{% static 'file-from-library.js' %}"></script>

and file-from-library.js loads fine but I get a 404 for file-from-project.js. What am I doing wrong here?

Directory structure with the referenced files:

myapp
  /myapp
    settings.py
    .
    .
    .
  /static
    file-from-project.js
    .
    .
    .
  /templates
    .
    .
    .
  manage.py
  .
  .
  .
wogsland
  • 9,106
  • 19
  • 57
  • 93

1 Answers1

0

OK, I see the problem I think.

STATIC_ROOT is an absolute path, not a relative one. if you set it to eg. BASE_DIR / "static" you might get what you want.

I usually set BASE_DIR at the top of my settings.py as it gets used for a few things:

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent

The other thing is that you have DEBUG=True which means it's going to be looking at your STATIC_URL under your app dirs. If you move the file to:

~/Projects/myapp/static/app_folder/file-from-project.js

It should work :)

Edit: I noticed myapp is an app, and not the project name, which makes me wonder what the project is called.

Your static files for development for this structure should probably be in:

~/Projects/myapp/static/myapp/<static file>

When you deploy, obviously DEBUG should be False, but here you should run collectstatic in order to gather all the static files in one place for deployment.

michjnich
  • 2,796
  • 3
  • 15
  • 31
  • `BASE_DIR` is set that way by default, but moving to `~/Projects/myapp/myapp/static/file-from-project.js` didn't help. Or do you mean something different by `app_folder`? – wogsland Jun 28 '21 at 09:35
  • Also, changing to `STATIC_ROOT = os.path.join(BASE_DIR, 'static')` doesn't have an effect on where things are getting pulled from. – wogsland Jun 28 '21 at 09:38
  • I mean that in your path `myapp` is the name of your django project, then that project contains django apps, each potentially with its own static files. – michjnich Jun 28 '21 at 09:39
  • Ah, wait, your app is `myapp` I see from `INSTALLED_APPS` . What is your django project called then? Not `Projects`??? Can you perhaps post your directory structure? – michjnich Jun 28 '21 at 09:41
  • Added directory structure. Production is fine, I'm just having an issue with local development at the moment. – wogsland Jun 28 '21 at 09:47
  • Ah, gotcha. Sorry, use of `myapp` as the project name threw me off. Also I realise I put thewrong path in my answer - `(your root)/myapp/static/myapp/file-from-project.js` is probably what you want ... I corrected the error in the answer – michjnich Jun 28 '21 at 09:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234277/discussion-between-wogsland-and-michjnich). – wogsland Jun 28 '21 at 09:57