在使用 django 设置 WSGI 期间为所有静态文件获取 404

Getting 404 for all static files during WSGI setup with django

我有以下目录结构

-----root
   |___docpd
      |__docpd (contains settings.py, wsgi.py , uwsgi.ini)
      |__static

在我在开发环境中设置 vanilla django 期间,一切都很好(用于加载的所有静态文件)。但是现在在设置 uwsgi 之后,我发现 none 我的静态文件正在加载(我收到 404 错误)。

我尝试了什么?

1. Read alot of stack overflow questions about this error and none could solve my problem.

2. I adjusted the python path with paths to my project and they seem to get added as i printed them out in the settings.py file, but still no luck.

一些代码

uwsgi.ini

    [uwsgi]
chdir=/home/ubuntu/docpad/Docpad/
wsgi-file=/home/ubuntu/docpad/Docpad/Docpad/wsgi.py
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/docpad.log

wsgi.py

    import os,sys
sys.path.append('/home/ubuntu/docpad/Docpad/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Docpad.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

settings.py

    from sys import path
from os.path import abspath, dirname, join, normpath

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
print "Base dir = " , BASE_DIR

DJANGO_ROOT = dirname(abspath(__file__))
print "Root =",DJANGO_ROOT

SITE_ROOT = dirname(DJANGO_ROOT)
print "SITE =", SITE_ROOT

path.append(DJANGO_ROOT)
print "Path = ",path

print BASE_DIR
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'SECRET'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

PROJECT_PATH = os.path.join(BASE_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)

TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'Docpad/templates/')
print "TEMPLATES = ",TEMPLATE_PATH

# TODO make separate template_paths
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    TEMPLATE_PATH,
)

...

在我 运行 我的应用程序之后,我得到 404 所有静态资源,如 css/js 文件。

更新

当我这样做时

python manage.py runserver 0.0.0.0:8000

服务器开始提供静态文件

但是,这个命令

uwsgi --ini uwsgi.ini --http :8000

给我问题(没有加载静态文件)。

我现在完全无能为力,一直在尝试各种替代方案,但没有运气。

如果有人能提供帮助,那就太好了。

在设置中

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.normpath(os.path.join(BASE_DIR, "static")),
)

在urls.py

    urlpatterns = [
#urls
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我通过以下操作解决了问题:

添加

check-static = /你的 django 目录/

在 uwsgi.ini 文件中。 它对我有用!

假设您的静态资产在 /customers/foobar/app001/public 下。在将请求传递给动态应用程序之前,您想检查每个请求在该目录中是否有相应的文件。 --check-static 选项适合您: --check-static /customers/foobar/app001/public

http://uwsgi-docs.readthedocs.io/en/latest/StaticFiles.html

你也可以这样做:

uwsgi --ini uwsgi.ini --http :8000 --static-map /static=/path/to/staticfiles/

或者将其添加到您的 .ini 文件中:

static-map = /static=/path/to/staticfiles

我不知道为什么,但当我替换此代码时它开始工作:

if settings.DEBUG:
    urlpatterns += [
        url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
    ]

有了这个:

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

在文件中 urls.py

我有 Django 1.8