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

问题描述 投票:2回答:4

我有以下目录结构

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

在开发环境中我的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

我是个i.朋友

    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.朋友

    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文件。

UPDATE

当我做

python manage.py runserver 0.0.0.0:8000

服务器开始提供静态文件

但是,这个命令

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

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

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

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

python django wsgi uwsgi
4个回答
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)

2
投票

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

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


1
投票

你也可以这样做:

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.