iFengo
iFengo

Reputation: 197

Django cannot find my static files

I am relatively new to web dev. and I am trying to build my first web application. I have my static folder in project_root/static but for some reason, I keep getting 404s when I run the server:

Not Found: /users/login/js/bootstrap.min.js for example.

I have {% load staticfiles %} at the top of my html and in my settings.py I have:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
STATIC_URL = '/static/'                                                                                        
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Thanks.


EDIT: Fixed with a combination of the answers below. Thank you all!

Upvotes: 7

Views: 15502

Answers (4)

Darwin
Darwin

Reputation: 2057

Use below code in settings.py

MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_ROOT = '/xxx/site/public/static/'
MEDIA_ROOT =  '/xxx/site/public/media/'

or Delete or comment DEBUG = True in settings.py

Upvotes: 1

Mohit Rustagi
Mohit Rustagi

Reputation: 316

Go ahead and read this- http://agiliq.com/blog/2013/03/serving-static-files-in-django/

Summary: If "users" is the app name in your project, then create directory - 'users/static/js/'. and put bootstrap.min.js into js/. This file will be accessible at localhost:8000/static/js/bootstrap.min.js. Go ahead and try it out.

Upvotes: 2

Vivek Singh
Vivek Singh

Reputation: 414

If DEBUG=True is settings.py it won't work. In such cases Django expects Apache/Nginx to take care of static files.

Upvotes: 2

theguru42
theguru42

Reputation: 3378

put your static js, css and other files in a directory called static inside your project directory then these settings will work:

# this defines the url for static files
# eg: base-url.com/static/your-js-file.js
STATIC_URL = '/static/'

# this is directory name where collectstatic files command will put your app level static files
STATIC_ROOT = 'staticfiles'

# this is directory paths where you have to put your project level static files
# you can put multiple folders here
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

based on these settings you will be able to access your static files correctly

Upvotes: 7

Related Questions