2017-10-08 119 views
-1

我试图去虽然Django的教程这也解释了关于静态文件 但静态文件时,我想实现的是我收到以下错误:的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项目结构:(删除不相关的文件和文件夹)。

[email protected] ~/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无法显示静态页面。

+1

你的文件被称为'style.css',但你尝试链接'sytle.css'。 – schwobaseggl

回答

1

错字:您使用sytle.css代替style.css

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

@ user2845672,你有没有试过?它对你有用吗? –