Django无法加载静态文件 [英] Django unable to load static files

查看:208
本文介绍了Django无法加载静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django的新手。

Completely new to django.

在我的settings.py文件中,我有:

In my settings.py file, I have:

STATIC_URL = '/static/'
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

我的urls.py有

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index)
]

我的views.py有

And my views.py has

def index(request):
    return render(request, 'index.html', {})

最后,我的index.html中包含以下内容:

Finally, I have the following in my index.html:

{% load static %}
<link href="{% static 'styles/custom.css' %}" rel="stylesheet">

HTML文件加载的方式与显示的一样,但样式未显示。经过检查,它表示未找到CSS文件。

The HTML file loads like it showed but the styles don't show up. On inspection, it says that the CSS file was not found.

这是我的目录结构:

.
├── README.md
├── app
│   ├── __init__.py
│   ├── db.sqlite3
│   ├── manage.py
│   ├── models.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── db.sqlite3
├── static
│   ├── scripts
│   │   └── custom.js
│   └── styles
│       └── custom.css
└── templates
    └── index.html


推荐答案

您必须告诉Django在哪里可以找到静态文件。通过在上设置 STATICFILES_DIRS 即可完成此操作settings.py 模块。

You have to tell Django where to find the static files. It is done by setting the STATICFILES_DIRS on your settings.py module.

通常,我们使用 BASE_DIR 做这样的事情:

Usually we do something like this, using the BASE_DIR:

settings.py

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






有关静态设置的一些额外信息:



一开始这有点令人困惑,因为Django具有三个类似的配置:


Some extra info on static settings:

It is a little bit confusing in the beginning, because Django has three similar configuration:


  • STATIC_URL

  • STATIC_ROOT

  • STATICFILES_DIRS

  • STATIC_URL
  • STATIC_ROOT
  • STATICFILES_DIRS

基本上, STATIC_URL 将用于生成URL,以提供服务您的静态资产。它可能是一个相对的URL,就像您曾经这样:

Basically, the STATIC_URL will be used to generate a URL to serve your static assets. It could be a relative URL like you had:

STATIC_URL='/static/'

{% static 'styles/custom.css' %} => /static/styles/custom.css

可能是完整的URL:

STATIC_URL='https://static.yourdomain.com/'

{% static 'styles/custom.css' %} => https://static.yourdomain.com/styles/custom.css

现在, STATICFILES_DIRS 是Django将在其中找到您的静态文件的地方。通常在项目文件夹中。但!这不是Django将为您的静态资产提供给客户端的地方。就像,静态文件的来源在哪里

Now, the STATICFILES_DIRS is where Django will find your static files. It's usually within the project folder. BUT! This is not from where Django will serve your static assets to the client. It's like, where is the source of your static files.

这是因为Django并未真正提供此类文件( .js .css .jpg )。好吧,无论如何都不是默认设置。您可以使用第三方应用,例如 WhiteNoise

That's because Django doesn't really serve this kind of files (.js, .css, .jpg). Well, not by default anyway. You could use a third-party app like WhiteNoise.

这也是为什么我们有 STATIC_ROOT 的原因,这是告诉Django STATICFILES_DIRS复制这些文件的位置,以便其他人可以为我服务。其他人通常是Apache或NGINX。

And that's also why we have the STATIC_ROOT, which is to tell Django where to copy those files from the STATICFILES_DIRS so someone else can serve it for me. This someone else is usually Apache or NGINX.

我们只使用 STATIC_ROOT 进行部署。

当我们将Django应用程序部署到生产服务器时,通常运行命令 python manage.py collectstatic 。这就是Django将文件从 STATICFILES_DIRS 复制到 STATIC_ROOT 的地方。

When we deploy a Django application to a productions server, we usually run the command python manage.py collectstatic. And that's where Django make a copy of the files from STATICFILES_DIRS to the STATIC_ROOT.

但是在开发阶段,您实际上不必关心这么多的细节,因为在 DEBUG = True 时,Django将使事情变得更容易您并提供静态文件。

But during the development phase you don't really have to care about this much detail, because while DEBUG=True, Django will make things easier for you and serve the static files.

这篇关于Django无法加载静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆