1
votes

I can't get my static files in Django to work with "namespaced" urls - I wonder if I'm missing something here?

my project structure

Basically I have one app called "foo" in my project "static_test". Inside that app I have a "static" folder which contains "test.txt".

I haven't changed anything in settings.py, so it contains all the default settings: staticfiles is included in INSTALLED_APPS, and STATIC_URL is set to '/static/'.

All of this is in full accordance with official Django tutorial: https://docs.djangoproject.com/en/1.9/howto/static-files/

I'm using Django 1.9.2.

When I start the Django development server, this is what I get:

localhost:8000/static/test.txt - 200 OK 

localhost:8000/static/foo/test.txt - 404 Page not found

This is despite the official tutorial claiming

In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).

Why is the second url (/static/foo/test.txt) not working for me? And why is the first url working? I do not have a project-wide static folder.

Thanks

2
What about your STATICFILES_DIRS - arcegk
I don't have them in my settings.py. This is how it comes by default when you start a project and I haven't touched that. - hazelsparrow

2 Answers

0
votes

from the docs

You should use 'django.contrib.staticfiles.finders.AppDirectoriesFinder' in your STATICIFILES_FINDERS variable in your settings.

0
votes

First one is searching under static. Second one under foo under static.

Your link should look like this.

 href="{% static "test.txt" %}"

And settings.py like this

STATIC_URL = '/static/'