使用passenger_wsgi在共享主机上的静态文件出现Django 403错误

问题描述 投票:0回答:3
我正在尝试使用 Passenger_wsgi 让我的 Django 应用程序在共享托管网站上运行(遵循托管提供商的指示)。

由于该应用程序适用于主域,我使用此文件夹布局安装它:

public_html django static css js images SMC_website templates
在settings.py中我有:

STATIC_URL = 'static/' STATICFILES_DIRS = [ '/home/xxxxxx/public_html/django/static/', '/home/xxxxxx/public_html/django/static/css', '/home/xxxxxx/public_html/django/static/js', '/home/xxxxxx/public_html/django/static/images', ]
在 urls.py 中我有:

urlpatterns = [ path('admin/', admin.site.urls), re_path(r"^$", views.index, name="Home Page"), ] urlpatterns += static(settings.STATIC_URL, document_root=os.path.join(settings.BASE_DIR, 'static'))
当我在 django manage.py shell 中尝试“static(settings.STATIC_URL,document_root=os.path.join(settings.BASE_DIR, 'static'))”时,我得到一个空列表 ([])。不知道为什么,或者这是否是我的问题的原因。

应用程序运行,从数据库中提取数据,但对于从静态文件夹(css、js 或图像)访问的任何内容都会生成 403 错误。

我有这些文件夹权限:

drwxr-x--- 2 xxxxxx xxxxxx 4096 Dec 17 18:30 css drwxr-x--- 8 xxxxxx xxxxxx 4096 Dec 17 18:30 images drwxr-x--- 3 xxxxxx xxxxxx 4096 Dec 17 18:30 js drwxr-xr-x 5 xxxxxx xxxxxx 4096 Dec 11 14:24 SMC_website drwxr-xr-x 3 xxxxxx xxxxxx 4096 Dec 11 14:24 logs -rw-r--r-- 1 xxxxxx xxxxxx 689 Dec 11 14:24 manage.py -rw-r--r-- 1 xxxxxx xxxxxx 1442 Dec 17 18:08 passenger_wsgi.py -rw-r--r-- 1 xxxxxx xxxxxx 245760 Dec 11 14:24 smcDB.sqlite3 drwxr-xr-x 10 xxxxxx xxxxxx 4096 Dec 11 14:31 static the xxxxxx is the User Name and it is the same as the folder in settings.py above
在浏览器调试器中,我看到静态内容的预期网址,例如:

<script type="text/javascript" src="https://southmetrochorale.org/static/js/smc.js"></script>
关于如何解决此 403 错误有什么建议吗?

python django static http-status-code-403
3个回答
2
投票
好的,首先您需要在

STATIC_ROOT

 内指定您的 
settings.py
。这声明了您要在其中提供静态文件的文件夹。

然后执行命令

python manage.py collectstatic

。这将从您的 
STATICFILES_DIRS
 中提取所有静态文件,并将它们组织到您之前在 
STATIC_ROOT
 下声明的文件夹中。

这部分:

urlpatterns += static(settings.STATIC_URL, document_root=os.path.join(settings.BASE_DIR, 'static'))
仅在 django-testserver 中使用 

DEBUG=True

您面临的问题是生产与开发。

为了进一步阅读,我推荐

如何在生产中部署静态文件


1
投票
我找到了解决这个问题的方法。因为 Django 应用程序位于主域位置 (public_html) -> public_html/django 的文件夹 (django) 中,而静态文件夹位于 public_html/django/static 我需要使用: STATIC_URL =“django/static/”

一切都很好,所有静态文件现在都已提供服务。


0
投票
我无法对 Volker Petersen 的回答发表评论,因此在此发帖。 我的项目位于一个文件夹内,并遵循相同的建议,即 STATIC_URL =“

/static/”,它得到了解决。

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