【问题标题】:django not able to load static filedjango 无法加载静态文件
【发布时间】:2018-03-19 22:42:03
【问题描述】:

我正在尝试阅读解释静态文件的 Django 教程 但是当我尝试实现时,我收到以下错误:

[08/Oct/2017 23:08:27] "GET / HTTP/1.1" 200 365
[08/Oct/2017 23:08:27] "GET /static/polls/sytle.css HTTP/1.1" 404 1658

我的 Django 项目结构:(删除不相关的文件和文件夹)。

rehman@linux-desktop ~/django_proj/my_site $ tree
.
├── db.sqlite3
├── manage.py
├── my_site
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── polls
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── models.py
│   ├── static
│   │   └── polls
│   │       ├── images
│   │       │   └── background.jpg
│   │       └── style.css
│   ├── templates
│   │   └── polls
│   │       ├── details.html
│   │       ├── index.html
│   │       └── result.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── templates

settigs.py(部分内容):

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

url.py:

urlpatterns = [
    url(r'^', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Polls</title>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'polls/sytle.css' %}" />
</head>
<body>
{% if latest_question_list %}
<ul>
    {% for q in latest_question_list %}
    <li><a href="{% url 'polls:details' q.id %}">{{ q.question_text }}</a></li>
    {% endfor %}
</ul>
{% else %}
    <p>no poll found</p>
{% endif %}

</body>
</html>

来自浏览器的 HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Polls</title>

    <link rel="stylesheet" type="text/css" href="/static/polls/sytle.css" />
</head>
<body>

<ul>

    <li><a href="/3/">Question 2</a></li>

    <li><a href="/2/">Question 1</a></li>

    <li><a href="/1/">Whats new?</a></li>

</ul>


</body>
</html>

我花了很多时间在网上搜索可能的陷阱并试图在这里找到我的问题,但我找不到任何解决方案。

任何人都可以通过 django 无法显示静态页面来解决我的代码中的任何错误或问题。

【问题讨论】:

  • 您的文件名为style.css,但您尝试链接sytle.css

标签: python django python-3.x django-templates django-static


【解决方案1】:

错字:你用sytle.css代替style.css

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
                                                              ^^^^^^

【讨论】:

  • @user2845672,你试过了吗?它对你有用吗??
猜你喜欢
  • 2014-07-12
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 2020-11-12
  • 2020-04-16
  • 2014-09-03
相关资源
最近更新 更多