【问题标题】:Django Debug False server 500, ValueError The file 'css/styles.css' could not be foundDjango Debug False server 500, ValueError 找不到文件'css/styles.css'
【发布时间】:2016-11-13 20:52:06
【问题描述】:

今天我在托管服务器上部署了我的项目,因为我有 DEBUG = False 我也有一个内部服务器错误。完整的报告是:

/storage.py", line 280, in stored_name
    cache_name = self.clean_name(self.hashed_name(name))
    File "/home/ttipprotest/lib/python3.4/django/contrib/staticfiles
/storage.py", line 94, in hashed_name
    (clean_name, self))
    ValueError: The file 'css/styles.css' could not be found with 
<django.contrib.staticfiles.storage.ManifestStaticFilesStorage object at 
0x7f2787c23ef0>.

在开发中一切正常,但现在我一整天都在尝试解决这个问题。很抱歉显示错误消息。

编辑:

在产品设置中:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

STATIC_URL = '/static/'
STATIC_ROOT = "/home/ttipprotest/webapps/ttipprotest_static/"
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static', 'our_static'),
)

MEDIA_URL = '/media/'
MEDIA_ROOT = "/home/ttipprotest/webapps/ttipprotest_media/"

项目结构:

src
-ttipprotest
--manage.py
--settings
---base.py
---prod.py
-app 1
-...
-app n
-static
--our static
---css
---js
---fonts
-templates

第二次编辑:

似乎每次渲染模板时都会出现问题。 这是 index.html。我想这是这种行为的根源,因为我发现没有这个 index.html 的其他应用程序工作正常。

{% load staticfiles %}
{% load crispy_forms_tags %}

【问题讨论】:

  • 你在哪里部署它?如果它是一些 VPS,您应该阅读有关使用 UWSGI 或 Gunicorn 和 Web 服务器(如 NGINX)进行部署的信息
  • 你读过docs.djangoproject.com/en/1.9/howto/static-files/deployment吗?开发环境和生产环境之间的区别在于,您需要告诉 Django 在哪里可以找到静态文件,将它们复制到哪里(如果使用通常的collectstatic 安排运行),以及服务器将在哪个 URL 上为它们提供服务。
  • 它是通过 apache2 托管的共享服务器。我按照主持人的建议设置了静态
  • @nimasmi。是的,我做到了。也许我跳过了一些东西。我将编辑我的产品设置
  • 如果您仍然卡住,那么我认为可以通过从您的生产设置文件中添加一些相关位来改善问题,告诉我们您的项目的目录结构是什么,并将错误回溯剥离为最后三行。

标签: django django-staticfiles django-static


【解决方案1】:

我在编辑时发现了错误。

index.html:

{% load staticfiles %}

DEBUG = True 完美配合。至少它正在渲染和显示模板,但在显示错误的日志文件中。 index.html 是大多数其他模板扩展的模板。

在我将 index.html 更改为:

{% load static %}

DEBUG = False 工作正常

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 2015-04-11
    • 2018-06-05
    • 2021-11-03
    • 2021-08-23
    • 2021-08-13
    • 2019-06-01
    • 1970-01-01
    相关资源
    最近更新 更多