python - Django : Static content not found

标签 python django django-staticfiles

一整天我都在为这个问题绞尽脑汁,但无法找出问题所在。它发生在我将我的项目从一台机器复制到另一台机器之后。

设置.py

STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

在 INSTALLED_APPS 中也提到了“django.contrib.staticfiles”。

文件夹结构:

Django-Projects (root)
    project
    app
    static
        css
          home.css
        js
    manage.py

模板:

{% load staticfiles %}

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

网址.py

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('app.urls')),
)

它在打开模板时在控制台中抛出错误:

 GET http://127.0.0.1:8000/static/css/home.css 
Failed to load resource: the server responded with a status of 404 (NOT FOUND)

这里可能出了什么问题?请帮帮我。非常感谢!

最佳答案

在你的 settings.py 中

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

然后在你的模板文件中:

<link rel="stylesheet" href="{{ STATIC_URL }}css/home.css">

根据您显示的目录根结构,我认为上述设置应该有效。虽然还没有测试过。让我知道它是否有效。

关于python - Django : Static content not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755457/

相关文章:

python - 将当前单击的按钮作为参数传递给 Kivy (Python) 中的 on_press

python - pandas 按列表查询行

jquery - 如何让数据表与 Django 模板中的 bootstrap4 选项卡一起使用?

python - 为django中的所有模板加载静态文件

django - {%load static%}和{%load staticfiles%}:哪个是首选?

javascript - 使用javascript重定向django url

Python psd_工具 : 'module' object has no attribute 'profile_frombytes'

python - 混合模式 CSV 导入 Pyspark

python - Django Rest框架-可写(创建,更新)双重嵌套序列化器

python - Django 服务器在 CONTROL-C 之后仍在运行