【问题标题】:Getting 404 for all static files during WSGI setup with django在使用 django 设置 WSGI 期间为所有静态文件获取 404
【发布时间】:2015-10-16 10:32:33
【问题描述】:

我有以下目录结构

-----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

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,
)

...

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

更新

当我这样做时

python manage.py runserver 0.0.0.0:8000

服务器开始提供静态文件

但是,这个命令

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

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

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

如果有人能帮忙,那就太好了。

【问题讨论】:

  • 你跑collectstatic了吗?
  • 是的,目录里全是图片
  • 此外,在我开始使用 uwsgi 之前,我不明白为什么它可以工作?
  • 您是尝试使用 uwsgi 本身提供静态文件还是使用任何 http 服务器?
  • 不,我希望 django 服务器提供这些文件,直到我安装 nginx

标签: python django wsgi uwsgi


【解决方案1】:

你也可以这样做:

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

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

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

【讨论】:

  • 叮叮叮!这就是这里的赢家。确保在执行此操作之前运行 django collectstatic 命令。将您的 django STATIC_ROOT 设置为 /path/to/staticfiles 以获取此帖子的示例
【解决方案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

【讨论】:

  • 感谢您的回答。我想我问这个问题的时间 --check-static 不可用。请在您的答案中添加注释,提及此适用的 uwsgi 版本。这在 1.9 中可用。
  • 我的uwsgi版本是2.0.15
  • django 目录还是应用程序静态目录?
【解决方案3】:

在设置中

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)

【讨论】:

  • 它说,名称静态未定义
  • docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/…。这是在不同的开发服务器中提供静态服务的方式
  • in urls import: from django.conf.urls.static import static
  • 太棒了,效果很好。但是等一下,我有一个关于你的答案的问题
  • 我已经在我的 settings.py 中添加了这两行,但是缺少 urls.py 内容。你能告诉我为什么当我运行“python manage.py runserver”而不是在 uwsgi --ini uwsgi.ini --http :8000
【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 2021-01-12
    • 1970-01-01
    • 2012-09-30
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多