【发布时间】:2020-11-29 14:40:00
【问题描述】:
Django 网站(django 版本 1.11.29)未从实时服务器上的静态文件夹加载 css 和 js 脚本。但是在本地加载。有什么想法吗?
to css 似乎是正确的,但没有转到 css 样式表本身。
链接到 localhost 主机上的 css:
<link rel="stylesheet" href="/static/website/login/login-style.css">
链接到实时服务器上的 CSS:
<link rel="stylesheet" href="/static/website/login/login-style.css">
settings.py
import os
# other code...
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
#updated
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL)
登录.html
<link rel="stylesheet" href="{% static 'website/login/login-style.css' %}">
静态文件夹结构:
blah\project\website\static\website\login
更新:
我意识到我需要通过python manage.py collectstatic 将所有文件复制到根静态文件夹以进行生产。
但是仍然没有加载。我错过了一步吗?更新设置.py
【问题讨论】:
-
Django 在生产环境中不提供静态文件。预计将由网络服务器完成。
-
collectstatic 仅将所有静态文件收集到在“STATIC_ROOT”上声明的目录中。您仍然需要告诉服务器静态文件的 URL。例如,在 pythonanywhere 上,您必须在 Web 应用设置中正确配置 URL 和 PATH。
-
你在哪里部署?
标签: python css django django-views django-templates