简体   繁体   English

从 Django 的根目录提供 static 文件

[英]Serving static files from root directory in Django

I have app level static files and want to serve global static files such as JQuery, my project structure is like this:我有应用程序级别的 static 文件并希望提供全局 static 文件,例如 JQuery,我的项目结构是这样的:

mysite
├───dashboard
│   ├───migrations
│   ├───static
│   │   └───dashboard
│   │       ├───css
│   │       ├───img
│   │       └───js
│   └───templates
│       └───dashboard
├───etc
├───static
│   └───js
|       └───jquery.min.js
└───mysite

I added the followed the django docs so that the settings.py looks我添加了 django 文档,以便 settings.py 看起来

STATIC_ROOT = ""

STATIC_URL = '/static/'

STATICFILES_DIR = [
    os.path.join(BASE_DIR, "static"),
    os.path.join("static"),
]

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

my urls.py looks like this我的 urls.py 看起来像这样

urlpatterns = [
    path('admin/', admin.site.urls),
    path("", include(dashboard.urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

my INSTALLED_APPS looks like this我的 INSTALLED_APPS 看起来像这样

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

when I try to access it from a template, it returns a response code 404 in the runserver log,当我尝试从模板访问它时,它会在 runserver 日志中返回响应代码 404,

[09/Apr/2020 01:59:34] "GET /static/js/jquery.min.js HTTP/1.1" 404 1674

the template tag is like this模板标签是这样的

{% load static %}
<script src="{% static 'js/jquery.min.js' %}"></script>

This is my first Django project without following a tutorial, and any help would be appreciated.这是我的第一个 Django 项目,没有遵循教程,任何帮助将不胜感激。 Thank You谢谢你

EDIT: I was able to solve the problem by removing the STATIC_ROOT setting编辑:我能够通过删除STATIC_ROOT设置来解决问题

You need to specify a prefix in front of one of the two directories named static because Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. You need to specify a prefix in front of one of the two directories named static because Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable来区分它们。 We need to be able to point Django at the right one, and the best way to ensure this is by namespacing them.我们需要能够将 Django 指向正确的位置,确保这一点的最佳方法是对它们进行namespacing That is, by putting those static files inside another directory named for the application itself.也就是说,将这些 static 文件放在另一个以应用程序本身命名的目录中。

You can namespace static assets in STATICFILES_DIRS by specifying prefixes .您可以通过指定prefixes在 STATICFILES_DIRS 中命名 static 资产。 For example:例如:

STATICFILES_DIR = [
    ("main", os.path.join(BASE_DIR, "static")),
    os.path.join("static"),
]

and then:接着:

<script src="{% static 'main/js/jquery.min.js' %}"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM