Pwan
Pwan

Reputation: 153

Django - Can't load static file (404 error)

I'm developing web with django framework.

So I have 3 folders in my static file like this.

*web
  *home
  *static
    *admin
    *scripts
    *styles
  *web
  *manage.py
  *db.sqlite3

and I have my setting file with this static setup

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

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

But when I type python3 manage.py collectstaitc I get this result

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/pwanhsu/django/django/core/management/__init__.py", line 349, in execute_from_command_line
    utility.execute()
  File "/Users/pwanhsu/django/django/core/management/__init__.py", line 341, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/pwanhsu/django/django/core/management/base.py", line 290, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/pwanhsu/django/django/core/management/base.py", line 341, in execute
    output = self.handle(*args, **options)
  File "/Users/pwanhsu/django/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
    collected = self.collect()
  File "/Users/pwanhsu/django/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/Users/pwanhsu/django/django/contrib/staticfiles/finders.py", line 112, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/Users/pwanhsu/django/django/contrib/staticfiles/utils.py", line 28, in get_files
    directories, files = storage.listdir(location)
  File "/Users/pwanhsu/django/django/core/files/storage.py", line 286, in listdir
    for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/Users/pwanhsu/Desktop/v_bridge/web/web/static'

I can successfuly collect static file if I remove the STATICFILES_DIRS like this

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

0 static files copied to '/Users/pwanhsu/Desktop/v_bridge/web/static', 56 unmodified.

In my html file, I have these codes

{% load staticfiles %}

But the server return me an 404 error

[08/Sep/2016 03:36:45] "GET / HTTP/1.1" 200 5831
[08/Sep/2016 03:36:45] "GET /static/scripts/bootstrap/css/bootstrap-responsive.min.css HTTP/1.1" 404 1763
[08/Sep/2016 03:36:45] "GET /static/scripts/icons/social/stylesheets/social_foundicons.css HTTP/1.1" 404 1775
[08/Sep/2016 03:36:45] "GET /static/scripts/icons/general/stylesheets/general_foundicons.css HTTP/1.1" 404 1781

Anyone has idea about how to set up the static path or what I did wrong. Really confuse with it.

Upvotes: 0

Views: 1100

Answers (4)

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

Your settings.py is in web/web/ directory. You static dir is in web/ directory. In this case set:

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

STATICFILES_DIRS defines the additional locations the staticfiles. You don't need this settings.

Upvotes: 1

ALLSYED
ALLSYED

Reputation: 1553

STATIC_ROOT='static'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
  os.path.join(SITE_ROOT ,'/static/'),
]

This is actually snippet from one of my projects.In the begining ,I have struggled with static files too.This should fix your problem.If you are still getting 404.Check the permissions on static files dir and static files

Upvotes: 0

Pierangelo Orizio
Pierangelo Orizio

Reputation: 824

Try to use BASE_DIR in staticfiles_dir:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

or

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'web/static')]

Upvotes: 0

Rajmani Arya
Rajmani Arya

Reputation: 104

Use STATICFILES_DIRS=('. /static',) or

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static' ), ) 

Upvotes: 1

Related Questions