【问题标题】:I am unable to load static files in django although i have done everything尽管我已经完成了所有工作,但我无法在 django 中加载静态文件
【发布时间】:2021-01-27 07:56:00
【问题描述】:

在我的 settings.py 文件中我做了

STATICFILES_LOCATION='static'
STATIC_URL = '/static/'
STATICFILES_DIR=[
os.path.join(BASE_DIR,'Kevin/static')
]
STATIC_ROOT='static'

但在我的模板中我无法加载文件 模板文件夹在根目录中 而静态文件夹位于名为 Kevin 的 django 根文件夹中,该文件夹也在该基本文件夹中 在我的 html 模板中,我做了

{% load static %}

我也分享了我的模板的一些代码,所以你可以看看 {% 加载静态 %}

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">

<title>Kevin - Dashboard</title>

<!-- Custom fonts for this template-->
<link href="{% static 'vendor/fontawesome-free/css/all.min.css'%}" rel="stylesheet" type="text/css">
<link
    href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
    rel="stylesheet">

<!-- Custom styles for this template-->
<link href="{%static 'css/sb-admin-2.min.css'%}" rel="stylesheet">

【问题讨论】:

  • 你运行过 python manage.py collectstatic 吗?
  • 它是在您的开发环境中还是在生产环境中?
  • 也许您可以提供您的项目树结构的屏幕截图以帮助我们更好地理解?
  • 显示你的主要 urls.py 代码@Azee
  • @GaëtanGR 是的,我做了两次

标签: django static


【解决方案1】:

你还需要在 urls.py 中设置一个静态 URL

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
   .....
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

阅读此https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development

【讨论】:

  • 没有任何区别
  • 添加你的 settings.py 所在的 URL @Azee
  • 如果你的INSTALLED_APPS 中有django.contrib.staticfiles,我猜你有,你不需要添加这个。
  • @c.grey 我已经在我的基本 urls.py 中添加了这些,但它仍然无法正常工作
【解决方案2】:
 STATICFILES_DIRS = [
BASE_DIR / "myappname/static",

 ]

这基本上为我解决了我拼错了上面的代码 我使用 STATICFILES_DIR 而不是 STATICFILES_DIRS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-31
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2015-11-09
    • 2019-03-24
    相关资源
    最近更新 更多