我试图通过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无法显示静态页面来解决我的代码上的任何错误或问题。
答案 0 :(得分:1)
错字:您使用sytle.css
代替style.css
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
^^^^^^