简体   繁体   English

Django 4.0.5:如何正确地将静态文件链接到模板中

[英]Django 4.0.5: How to properly link static files into templates

I'm following the CS50W course, and I noticed a weird behavior when going through the Django lecture.我正在学习 CS50W 课程,在进行 Django 讲座时,我注意到一个奇怪的行为。 In summary, I was getting a 404 error when loading the styles.css file on one of the apps.总之,在其中一个应用程序上加载 styles.css 文件时出现 404 错误。

After checking the lecture source code, the main difference was that the lecture used the following to define STATIC_URL:在查看了讲座源代码后,主要区别在于讲座使用以下内容来定义 STATIC_URL:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = '/static/'

And noticed that even Django's documentation defines STATIC_URL as STATIC_URL = 'static/' , and somehow, adding that slash at the beginning fixes the 404 issues.并注意到,即使 Django 的文档也将 STATIC_URL 定义为STATIC_URL = 'static/' ,并且不知何故,在开头添加斜线可以解决 404 问题。 Could someone explain to me why that is?有人可以向我解释为什么会这样吗?

For reference, this is the project structure I have (the issue is within the app called newyear):作为参考,这是我拥有的项目结构(问题在名为 newyear 的应用程序中):

.
├── db.sqlite3
├── manage.py
├── myapp
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── admin.cpython-38.pyc
│   │   ├── apps.cpython-38.pyc
│   │   ├── models.cpython-38.pyc
│   │   ├── urls.cpython-38.pyc
│   │   └── views.cpython-38.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-38.pyc
│   ├── models.py
│   ├── templates
│   │   └── myapp
│   │       ├── greet.html
│   │       └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── newyear
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── admin.cpython-38.pyc
│   │   ├── apps.cpython-38.pyc
│   │   ├── models.cpython-38.pyc
│   │   ├── urls.cpython-38.pyc
│   │   └── views.cpython-38.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-38.pyc
│   ├── models.py
│   ├── static
│   │   └── newyear
│   │       └── styles.css
│   ├── templates
│   │   └── newyear
│   │       └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── prueba
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-38.pyc
    │   ├── settings.cpython-38.pyc
    │   ├── urls.cpython-38.pyc
    │   └── wsgi.cpython-38.pyc
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Template linking the styles:链接样式的模板:

<!DOCTYPE html>
<html lang="en">
    <head>
        {% load static %}
        <link rel="stylesheet" href="{% static 'newyear/styles.css' %}">
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Is it new year?</title>
    </head>
    <body>
        {% if newyear %}
            <h1>Yes</h1>
        {% else %}
            <h1>No</h1>
        {% endif %}
    </body>
</html>

Side note: I know this might be a basic question and I can just use what works, which is adding the slash when defining STATIC_URL, however, I would really like to understand if this is the actual proper way of doing it, and if so, why the default settings.py file comes to set up in a way that it requires the addition of the slash.旁注:我知道这可能是一个基本问题,我可以只使用有效的方法,即在定义 STATIC_URL 时添加斜线,但是,我真的很想了解这是否是实际正确的做法,如果是的话,为什么默认的 settings.py 文件以需要添加斜杠的方式进行设置。

Prior to Django 4.0 the default was, in fact, '/static/' as described by the docs .实际上,在 Django 4.0 之前,默认值是 '/static/',如docs所述。

There have been a couple of changelog entries regarding it (relatively) recently.最近(相对)有几个关于它的更新日志条目。

3.1 The STATIC_URL and MEDIA_URL settings set to relative paths are now prefixed by the server-provided value of SCRIPT_NAME (or / if not set). 3.1设置为相对路径的 STATIC_URL 和 MEDIA_URL 设置现在以服务器提供的 SCRIPT_NAME 值作为前缀(或 / 如果未设置)。 This change should not affect settings set to valid URLs or absolute paths.此更改不应影响设置为有效 URL 或绝对路径的设置。

4.0 To allow serving a Django site on a subpath without changing the value of STATIC_URL, the leading slash is removed from that setting (now 'static/') in the default startproject template. 4.0为了允许在不更改 STATIC_URL 的值的情况下在子路径上为 Django 站点提供服务,从默认 startproject 模板中的该设置(现在为“静态/”)中删除了前导斜杠。

When I've changed the setting value in 4.0 to remove or add the forward slash, it hasn't made a difference, but if the course files settings.py has been changed from the default, it may be to accommodate some other bespoke changes that are part of the course.当我更改了 4.0 中的设置值以删除或添加正斜杠时,它并没有什么不同,但是如果课程文件 settings.py 已从默认值更改,可能是为了适应一些其他定制的更改这是课程的一部分。

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

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