【问题标题】:pythonanywhere django static files are not collected from applicationpythonanywhere django 静态文件不是从应用程序中收集的
【发布时间】:2021-04-05 16:54:38
【问题描述】:

问题是来自 Django 应用程序的静态文件未在 pythonanywhere 中收集。 命令后

python manage.py collectstatic

目录中

/home/user/user.pythonanywhere.com/static

仅显示 admin 文件夹。 设置.py:

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp'
]

css文件位置:

+---myapp
|   +---static
|   |   \---myapp
|   |       \---css
|   |           \---myapp.css

当我在本地命令行中运行命令 python manage.py collectstatic 时,会收集来自应用程序的静态文件,问题出在 pythonanywhere。

所有动作都是标准的,我都严格按照指南做的,我不明白这是怎么回事。非常感谢

我已经阅读了有关 pythonanywhere 的所有文章以及我在 stackoverflow 和文档中找到的所有内容,但没有任何帮助解决问题。

https://help.pythonanywhere.com/pages/DjangoStaticFiles
https://help.pythonanywhere.com/pages/DebuggingStaticFiles/
https://help.pythonanywhere.com/pages/StaticFiles

更新: BASE_DIR = Path(文件).resolve().parent.parent

Bash 控制台文本输出:

You have requested to collect static files at the destination location as specified in your settings: /home/user/user.pythonanywhere.com/static This will overwrite existing files! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes 0 static files copied to '/home/user/user.pythonanywhere.com/static', 132 unmodified. 

collectstatic命令后/home/user/user.pythonanywhere.com/static目录的内容:

+---admin
|   +---css
|   +---fonts
|   +---img
|   +---js

【问题讨论】:

  • BASE_DIR 的值是什么? collectstatic 不会生成任何关于发生的事情的文本输出吗?
  • BASE_DIR = Path(file).resolve().parent.parent 您已请求在设置中指定的目标位置收集静态文件:/home/user /user.pythonanywhere.com/static 这将覆盖现有文件!你确定要这么做吗?键入“yes”继续,或“no”取消:yes 0 静态文件复制到“/home/user/user.pythonanywhere.com/static”,132 未修改。 132个静态文件它是管理静态文件。在我的本地服务器上,他收集了 133 个静态文件和一个包含我需要的 css 文件的附加文件夹
  • 请更新您的问题并提供更多详细信息,请勿发表评论。看看这个答案stackoverflow.com/questions/66437690/…
  • 我看到您对 css 文件位置(我相信它取自 PythonAnywhere 帮助页面?)和运行 collectstatic 管理命令的位置使用了不同的结构。为了避免混淆:你能检查一下/home/user/user.pythonanywhere.com/static 目录的内容吗?
  • 我想主要问题是我在 .gitignore 文件中添加了 /static 行?这就是为什么 pythonanywhere bash 控制台看不到静态文件的原因吗?因为他们没有在github上下载?比上传静态文件的过程是什么?你能解释一下吗?我也阅读了这篇文章,但仍然不明白:docs/3.1/ref/contrib/staticfiles/docs/3.1/howto/static-files/deployment/ @ivan-starostin 我已经阅读了你的答案。 STATIC_URL - 好的,STATIC_ROOT - 好的。我不需要 STATICFILES_DIRS,因为默认的 STATICFILES_FINDERS 设置会找到 app 的静态子目录。

标签: python django pythonanywhere django-staticfiles


【解决方案1】:

在 PythonAnywhere 上,除了用于静态文件的常规 Django 设置之外, 您需要执行额外的步骤来设置静态文件映射 在网页上(您的网络应用的配置页面)。

  • 转到 PythonAnywhere 仪表板上的 Web 选项卡
  • 转到静态文件部分
  • 在 url 部分输入与 STATIC_URL 相同的 URL(通常为 /static/
  • 将 STATIC_ROOT 的路径输入到路径部分(完整 路径,包括/home/username/etc)
  • 然后点击重新加载并通过检索一个已知的静态文件来测试您的静态文件映射。

作为健全性检查,例如,如果您在 /home/myusername/myproject/static/css/base.css 有一个文件,请访问 http://www.your-domain.com/static/css/base.css

更多详情请见the docs

【讨论】:

    【解决方案2】:

    显示或加载静态文件只需要 2 个步骤:

    1. 打开项目设置在STATIC_URL后面加一行,粘贴STATIC_ROOT = '/home/QuizApp345/quiz_project/static'这一行(必须是你的项目目录REPLACE MINE)
    2. 在 bash 中输入 python manage.py collectstatic
    3. 在 pythonanywhere 的 Web 应用设置中添加静态文件路径。 在这些步骤重新加载网络应用程序后,您的静态文件现在应该被加载了。

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 2020-11-18
      • 2018-07-21
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多