简体   繁体   English

Django 2.2不提供静态文件

[英]Django 2.2 Not Serving Static Files

I am currently working through some tutorials to enhance my knowledge of Django. 我目前正在通过一些教程来增强对Django的了解。 In doing so, I decided to use to most recent version of Django. 为此,我决定使用最新版本的Django。 I have been able to overcome most of the divergences between my code and that of the tutorial except for one - static files are not being served. 我可以克服我的代码与教程代码之间的大部分差异,唯一的区别是-不提供静态文件。 Now, I am not fully sure that the difference is strictly due to the Django version. 现在,我不能完全确定差异是否完全取决于Django版本。 Here is what is going on. 这是怎么回事。

I have the following project structure: 我有以下项目结构:

在此处输入图片说明

settings.py settings.py

STATIC_URL = '/static/'

STATIC_FILES = (
    os.path.join(BASE_DIR, "static"),
)

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

What is interesting is that with this configuration, the project is able to find templates successfully. 有趣的是,使用此配置,项目能够成功找到模板。 However, as mentioned above, it cannot find the static files. 但是,如上所述,它找不到静态文件。 Why is that? 这是为什么?

Also, this tutorial sets up an unusual configuration in that the static and templates directories are located inside the main app's directory, whereas I typically have them one level higher, ie, in the project root's directory. 另外,本教程还设置了一个不寻常的配置,其中statictemplates目录位于主应用程序的目录内,而我通常将其高一级,即位于项目根目录下。 Could this be a part of the problem? 这可能是问题的一部分吗? What in the settings of this particular project makes Django look for files one directory lower? 在这个特定项目的设置中,什么使Django查找文件的目录低了一个目录? The settings seem the same as those of other project I had done. 设置似乎与我完成的其他项目相同。 However, those project contained the static and templates directories in the project's root directory. 但是,这些项目在项目的根目录中包含static目录和templates目录。

Any help and clarification is appreciated. 任何帮助和澄清表示赞赏。

Edit: 编辑:

urls.py urls.py

from django.contrib import admin
from django.urls import path
from .views import home

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

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

try replacing this: 尝试替换此:

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

with

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

Also provide the status of files (like 404 messages) 还提供文件的状态(例如404消息)

To serve static files you need to add the following configuration in urls.py as: 要提供静态文件,您需要在urls.py添加以下配置为:

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Next, I believe it is a good practice to put static and templates within their own apps, this is useful if you want to make that app plug-able any near future. 接下来,我相信将statictemplates放入自己的应用程序中是一个好习惯,如果您希望在不久的将来使该应用程序可插拔 ,这将非常有用。

Or it is also a good practice to gather all static files in project level directory and add the following in settings to identify it. 或者,将所有静态文件收集在项目级别目录中并在设置中添加以下内容以进行标识也是一种很好的做法。

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

edit: I just see the updated question. 编辑:我只看到更新的问题。 Try adding this: 尝试添加以下内容:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home, name='home')
]

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

This says, as long as we are in debug mode serve the static files. 这就是说,只要我们处于调试模式,就可以提供静态文件。

STATIC_FILES doesn't mean anything to Django, as far as I know, although maybe you are using it for another purpose. 据我所知,STATIC_FILES对Django没有任何意义,尽管您可能会将其用于其他目的。

The way you had STATIC_ROOT defined it was pointing to a non-existent directory. 您用STATIC_ROOT定义的方式指向的目录不存在。

There is a good reference in StackOverflow here 有一个在StackOverflow的一个很好的参考这里

尝试这个:

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

I was able to solve the issue by changing the settings to the following: 通过将设置更改为以下内容,可以解决此问题:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

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

Furthermore, it was helpful to read the following documentation when trying to understand how to fix it 此外,在尝试了解如何解决此问题时阅读以下文档很有帮助。

https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-STATICFILES_FINDERS https://docs.djangoproject.com/zh-CN/2.2/ref/settings/#std:setting-STATICFILES_FINDERS

Particularly, this explained clearly what Django did: 特别是,这清楚地解释了Django所做的事情:

The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). 默认设置将查找存储在STATICFILES_DIRS设置(使用django.contrib.staticfiles.finders.FileSystemFinder)和每个应用程序的静态子目录(使用django.contrib.staticfiles.finders.AppDirectoriesFinder)中的文件。 If multiple files with the same name are present, the first file that is found will be used. 如果存在多个具有相同名称的文件,则将使用找到的第一个文件。

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

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