Django部署没有在Google应用程序引擎中加载静态文件



嗨,我正在尝试在谷歌应用程序引擎上部署django应用程序。我的Django应用程序在本地运行良好,但在谷歌应用程序引擎中不起作用。我检查了一下,发现问题出在我的静态文件上。我的静态文件没有加载到应用程序引擎中。

********.appspot.com/static/youtube/about.css
Not Found
The requested resource was not found on this server.

我已经两天试图在各种论坛上关注答案了,但没有一个对我有效

我的设置.py代码快照

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']

# Application definition
INSTALLED_APPS = [
'youtube',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sitemaps',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

仅供参考,我尝试将STATIC_ROOT更改为"STATIC",但没有成功。


STATIC_ROOT = 'static'
STATIC_URL = '/static/'

我的目录结构:

我的项目目录结构

我的HTML文件来自应用程序youtube和应用程序目录结构

关于页面html文件快照

<link rel="stylesheet" type="text/css" href="{% static 'youtube/about.css' %}">

我的App.yaml

runtime: python
env: flex
runtime_config:
python_version: 3.7
entrypoint: gunicorn -b :$PORT yt_analytics.wsgi
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto

我已经运行以下命令在本地服务器上进行测试,然后在谷歌应用程序引擎上进行部署。

python manage.py runserver
python manage.py collectstatic
gcloud app deploy

当我运行collectstatic命令时,我看到它正在我的项目根目录中创建一个名为static的新文件夹。。即yt_analytics/static

现在我看到我的项目中有两个静态目录——

yt_analytics/static
yt_analystic/youtube/static

当我运行findstatic命令时,我得到了:

python manage.py findstatic youtube/about.css
Output:
Found 'youtube/about.css' here:
/home/work/Mirror/Projects/django/ytwebsite/yt_analytics/youtube/static/youtube/about.css

在问这个问题之前,已经用尽了所有论坛和答案。我无法在GAE上部署我的应用程序。非常感谢你的帮助。

在@Ivan Starostin的评论后在urls.py中添加以下代码,对我有效。


from django.conf.urls import url, include
from django.views.static import serve
urlpatterns = [
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]

感谢Ivan的宝贵意见。

最新更新