【问题标题】:Gunicorn cant find the static filesGunicorn 找不到静态文件
【发布时间】:2021-06-20 07:10:07
【问题描述】:

通过使用来自digitalcloud tutorial 的不带 nginx 的 gunicorn 我的服务器正在运行,并且在控制台上是

not found: /static/style.css

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

我已经试过了

  1. 收集静态

  2. 在 urls.py 文件中做urlpatterns += staticfiles_urlpatterns()

  3. makemigrations + 迁移

【问题讨论】:

  • 如果 DEBUG=False Django 不提供静态文件。

标签: django gunicorn django-staticfiles


【解决方案1】:

也许 django 尝试阅读 'static//static/style.css' 但你需要这个:'style.css' ;) ?

【讨论】:

  • 在 html 中我正在连接就像 {% static 'style.css' %} 在本地主机上工作
  • 我使用了这个配置: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file))) , STATIC_ROOT = os.path .join(BASE_DIR, '静态')
  • BASE_DIR = Path(file).resolve().parent.parent
  • 看看你的 nginx.conf 你有吗?位置 /static/ { 别名 /home/projects/nameproject/static/; }
  • 但是当我使用 runserver 0.0.0.0:port 运行时,静态文件仍然无法正常工作
【解决方案2】:

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

urlpatterns = [你的网址] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

【讨论】:

    【解决方案3】:

    您可以在项目文件夹中创建设置,然后在其中创建名称等设置文件: 本地.py

    from my_project.settings import *
    DEBUG = True
    ALLOWED_HOSTS = []
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    

    这在 VM 中运行,例如:python manage.py runserver --settings settings.local

    记得在网址中添加:

    if settings.DEBUG == True:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    web.py

    from my_project.settings import *
    DEBUG = False
    ALLOWED_HOSTS = ["www.side.com", "side.com"]
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    

    然后在 gunicorn 中仅导入 web.py 文件项目以在 nginx 中读取

    这在 VPS 服务器中运行。

    【讨论】:

    • 是的,我知道,但它不能解决静态文件问题
    猜你喜欢
    • 2021-11-16
    • 2019-02-12
    • 2016-06-01
    • 2017-11-08
    • 2018-02-10
    • 2012-12-06
    • 2013-12-09
    • 2020-01-04
    相关资源
    最近更新 更多