简体   繁体   中英

Django admin static files only -> 404

I have a django project that works fine with the embedded server, but when I try to use it on Apache/mod_wsgi, the static files of the admin pages (/static/admin/css) are not found (404). Here are my settings:

MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = '/var/www/wsgi/myproject/static'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = ()

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )


TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    )

TEMPLATE_CONTEXT_PROCESSORS = (
    "socialauth.context_processors.facebook_api_key",
    'django.core.context_processors.media',
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.request",
    "django.core.context_processors.static",
    )

ROOT_URLCONF = 'myproject.urls'

TEMPLATE_DIRS = ('/var/www/wsgi/myproject/templates',)

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

I also tried running

sudo python ./manage.py collectstatic

but with no luck.

What does you apache virtualhost config look like? You should include an Alias directive for the STATIC_ROOT path.

Something along these lines:

Alias /static [project-path]/static
Alias /media [project-path]/media
<Directory [project-path]/static>
    Order deny,allow
    Allow from all
</Directory>
<Directory [project-path]/media>
    Order deny,allow
    Allow from all
</Directory>

As you've written, you would need to run ./manage collectstatic each time you want to deploy new static files in production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM