Django Static files 404

2019-01-16 10:13发布

I can't get my static files to come up. I've tried various settings and directory configurations and so on, but they just turn up as 404s. I have debug_toolbar installed so know that STATIC_URL is reaching my request context.

Directory structure showing /static (I have also placed the directory inside of the meals app folder, and users, just to try it out.

/mealmate
    /mealmate
    /meals
    /static
        /css
             /bootstrap.min.css
    /templates
    /users

Settings.py (a few important settings though I've experimented with a variety of other ones):

MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media/')

STATIC_URL = '/static/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

WSGI_APPLICATION = 'mealmate.wsgi.application'

In base.html rendered

    <link rel="stylesheet" href="/static/css/bootstrap.min.css">

Any ideas? Thanks

9条回答
Summer. ? 凉城
2楼-- · 2019-01-16 10:29

I'm assuming you're using Django1.3+ here.

First off, you need to define a few more settings:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

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

STATIC_ROOT = path.join(TOP_DIR, 'staticfiles')
STATIC_URL = '/static/'

This should help you find that directory.

Also, you should always access your static files using STATIC URL:

<link rel="stylesheet" href="{{ STATIC_URL }}css/bootstrap.min.css">
查看更多
我只想做你的唯一
3楼-- · 2019-01-16 10:35

Make sure mealmate is in your INSTALLED_APPS

查看更多
别忘想泡老子
4楼-- · 2019-01-16 10:41

I was also stuck in the 404 problem until I realized that Apache had blocked the requests to the static dir. I used python manage.py collectstatic to copy all the static files to the static dir under my project root, i.e. /var/my/site/static. With

Alias /static /var/my/site/static
<Directory /var/my/site/static>
        Require all granted
</Directory>

in /etc/httpd/conf/httpd.conf, it works properly now.

If none of the answers above works, you may consider checking your server config.

查看更多
登录 后发表回答