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

时间:2015-10-16 10:32:33

标签: python django wsgi uwsgi

我有以下目录结构

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

在dev环境中设置我的vanilla django时,一切都很好(所有静态文件都用于加载)。但是现在设置uwsgi后,我发现我的静态文件都没有被加载(我收到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

给我提问(不加载静态文件)。

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

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

4 个答案:

答案 0 :(得分:2)

在设置中

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)

答案 1 :(得分:1)

我通过以下方式解决了问题:

添加

check-static = /你的django dir /

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

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

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

答案 2 :(得分:1)

您也可以这样做:

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

或者只是将其添加到.ini文件中:

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

答案 3 :(得分:0)

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

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