我不能为我的生活搞清楚为什么我django找不到我的静态文件。我的目录结构是这样的:
├── projectname
│ ├── appname
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ │ └── __init__.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── manage.py
│ └── projectname
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ └── settings.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── static
├── templates
└── virtualenv
我的appname / urls.py看起来像这样
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.home_page, name="home_page")
]
appname / views.py:
from django.shortcuts import render
def home_page(request):
return render(request, 'home_page.html')
home_page视图扩展了base.html,位于./templates/base.html
{% load staticfiles %}
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'home_page.css' %}">
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
home_page.html位于./templates/home_page.html
{% extends 'base.html' %}
{% block content %}
<p>TEXT</p>
{% endblock %}
最后是./static/styles.css上的css文件
p {
color: red;
}
以下是projectname / settings.py
中的模板和静态设置TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '../templates/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_ROOT = os.path.join(BASE_DIR, '../static/')
STATIC_URL = '/static/'
我尝试了大多数静态和许多os.path.join选项的排列。但是我似乎无法正确链接到该文件,在运行服务器时总是得到404。我做错了什么?