django - 为什么 Django 不加载内置静态文件?
问题描述
我在通过 uWSGI hunky dory 运行的 Apache 服务器上设置了一个 Django 实例。当我使用python manage.py runserver
. 不幸的是,在我将文件上传到预先准备好的生产服务器的那一刻,它拒绝加载所有 Django 的内置静态文件,返回 404,我不知道为什么......
正在查询的 url 被列为www.mysite.com/static/admin/css/fonts.css
并且与管理后端(css/base.css
和css/dashboard.css
)类似css/responsive.css
。起初我认为软件包可能有问题,所以我强制重新安装了 Django,但没有成功。还没加载...
这是我的 uWSGI 配置 .ini:
[uwsgi]
chdir = /path/to/top/level/mysite
module = mysite.wsgi:application
env = DJANGO_SETTINGS_MODULE=mysite.settings
master = true
pidfile = /path/to/project.pid
socket = 127.0.0.1:49152
; Dont use UNIX sockets as it confuses the proxy and alters the request
: URL's. Current Apache version cant handle it.
; socket=/var/run/swerth_django.sock
processes = 5
harakiri = 20
post-buffering = 1
max-requests = 5000
vacuum = true
home = /path/to/top/level/Virtualenv/directory
daemonize = /path/to/uWSGI.log
env = LANG=en_US.UTF-8
enable-threads = true
我已经包含了静态根和 URL 如下(评论是为了我自己的理解):
#The URL of which the static files in STATIC_ROOT directory are served
STATIC_URL = '/static/'
#The absolute path to the directory where ./manage.py collectstatic
# will collect static files for deployment.
STATIC_ROOT = '/home/swerth/public_html/swerth/static'
值得一提的是,到目前为止,我还没有自己的任何静态文件。我不在发展的那个阶段。我只是想使用 Django 附带的模板。
编辑:
好的,所以我做了一些研究并设法缩小范围(感谢@Alasdair指出 Django 文档中的特定页面)。看来我可以运行python manage.py collectstatic
得很好,它将我需要的所有文件导入到www.mysite.com/static
. 然而,出于某种原因,django dosnt 似乎正在“看到”这些文件,因为它们没有在浏览器中呈现(404 未找到)。
鉴于此,我假设它是我的配置settings.py
?
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
#The URL of which the static files in STATIC_ROOT directory are
#served
STATIC_URL = '/static/'
#The absolute path to the directory where ./manage.py collectstatic
# will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
我的 Apache 使用以下配置设置为反向代理:
RewriteEngine off
ProxyPreserveHost on
ProxyPass "/" "uwsgi://127.0.0.1:49152/"
ProxyPassReverse "/" "uwsgi://127.0.0.1:49152/"
我做错了吗?(Ps 我试图避免编辑 HTML 文件的静态文件链接,因为我真的很想知道我做错了什么,所以它不会再次发生,因为 Django 应该能够直接从www.mysite.com/static
.
编辑第 2 轮:
好的,所以我按照建议做了,并尝试设置我的代理以排除静态目录并正常提供文件并添加别名以将 URL 映射到实际目录,但它仍然无法正常工作。ProxyPass
即使我将它包含在不太具体的规则之上,它似乎完全忽略了我的异常并将其发送给 Django?有趣的问题,当我指定/admin/
而不是仅仅/
针对不太具体ProxyPass
的/admin/
. 此外,我403
尝试提供的静态文件被禁止使用?!?!我当前的 .conf 看起来像这样:
Alias /static/ /home/swerth/public_html/swerth/static/
<Directory /home/swerth/public_html/swerth/static/>
Require all granted
</Directory>
<Directory /home/swerth/public_html/swerth/swerth/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ProxyPreserveHost on
ProxyPass /whm-server-status/ !
ProxyPass /static/ !
ProxyPass / uwsgi://127.0.0.1:49152/
ProxyPassReverse / uwsgi://127.0.0.1:49152/
解决方案
这可能是因为您没有设置 Apache 来提供静态文件。尝试执行以下操作
- 转到您的“静态”文件夹并使用
pwd
. 看起来像/home/swerth/public_html/swerth/static
你的情况。还要确保运行 Apache httpd 进程的用户对静态文件夹具有读取权限。 现在通过执行编辑您的 VirtualHost
sudo nano /etc/apache2/sites-available/000-default.confsudo nano /etc/apache2/sites-available/000-default.conf
如果您使用不同的配置文件,请将 000-default.conf 替换为您自己的。将以下内容添加到虚拟主机块:
<VirtualHost *:80>
. . .
Alias /static /home/swerth/public_html/swerth/static
<Directory /home/swerth/public_html/swerth/static>
Require all granted
</Directory>
. . .
</VirtualHost>
还要确保将其放在 uwsgi<Directory>
第 4 部分上方。通过执行重新启动 apachesudo systemctl restart apache2
这是我找到的关于如何将 Django 应用程序部署到生产环境的很好的教程。
希望这可以帮助。
推荐阅读
- typescript - 负类型断言返回谓词
- flutter - 如何在 Flutter 的另一个全宽屏幕上创建一个 1/3 宽度的屏幕?
- swift - SwiftUI:使用计时器时,每次的平均执行次数会保持不变吗?
- reactjs - React 功能组件 UseEffect 不
- amazon-web-services - Athena bigint 和 double 在同一列上有错误
- installation - 使用 Wix 重命名现有文件夹
- python-3.x - [:] 在覆盖 for 循环中的列表中的作用是什么?
- javascript - 无法读取未定义的属性(读取“setState”)
- python - 使用 pip 卸载具有依赖项的软件包
- quote - VBX 转义多个双引号