【问题标题】:I've read the documentation but my STATIC folder is still not being served我已阅读文档,但我的 STATIC 文件夹仍未提供服务
【发布时间】:2020-04-15 19:14:55
【问题描述】:

我的 settings.py 文件设置了静态 URL:

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

STATIC_URL = '/static/'

静态文件应用已安装:

INSTALLED_APPS = [
    'Journal',
    'adminsortable2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

我在我的应用中创建了一个名为 static/journal 的文件夹:

https://i.stack.imgur.com/T1eGw.png

我的模板加载静态并根据文档使用 URL:

{% load static %}
<link rel='icon' href='{% static "journal/favicon.ico" %}' type='image/x-icon'>

然而,当我浏览到 http://localhost/static/journal/favicon.ico 时,我得到一个错误 404!!!

我的应用程序的其余部分可通过http://localhost 访问(运行服务器位于端口 80 上,本地主机通过主机文件解析为 127.0.0.1_

【问题讨论】:

    标签: django


    【解决方案1】:

    您需要将静态文件url添加到urlurls.py

    在你的 urls.py 中:

    在文件的顶部放置这些行:

    from django.views.decorators.cache import cache_control
    from django.contrib.staticfiles.views import serve
    from django.conf import settings
    from django.conf.urls.static import static
    

    在文件的底部放置这些行:

    if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL,
                          view=cache_control(no_cache=True, must_revalidate=True)(serve))
    

    【讨论】:

      【解决方案2】:

      从您的问题看来,您似乎没有使用 url 中的端口号。你可以试试http://localhost:80/static/journal/favicon.ico .

      【讨论】:

        猜你喜欢
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-08
        • 2018-03-10
        • 2020-05-19
        相关资源
        最近更新 更多