我已经阅读了静态文件上的无限线程以及似乎发生的所有问题,但没有解决我的问题。我花了很多时间让我的“媒体”文件夹在昨天工作,但我似乎无法让静态文件工作。
我的模板在文件的正确位置查找,文件在那里,但是当模板尝试加载时,我得到404.
这是我的settings.py:
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
MEDIA_URL = '/media/'
MEDIA_ROOT = 'public/media/'
STATIC_URL = '/static/'
# STATIC_ROOT = 'public/static/'
STATIC_DIRS = (
STATIC_PATH,
)
base.html文件
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Address Book</title>
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}address_book/stylesheets/1.css"
</head>
{% if user.is_authenticated %}
<h1>Hello {{ user.username }}!</h1>
{% else %}
<h1>Please login below</h1>
{% endif %}
<body>
{% block body_block %}{% endblock %}
</body>
<h2> Need to make changes? </h2>
<ul>
<li><a href="{% url 'index' %}">Go home</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'logout' %}">Logout</a></li>
<li><a href="{% url 'add_client' %}">Add new client</a></li>
{% else %}
<li><a href="{% url 'login' %}">Login</a></li>
<li><a href="{% url 'register' %}">Register here</a></li>
{% endif %}
</ul>
urls.py
from django.conf.urls import patterns, url
from address_book import views
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^add_client/$', views.add_client, name='add_client'),
url(r'^register/$', views.register, name = 'register'),
url(r'^login/$', views.user_login, name='login'),
url(r'^logout/$', views.user_logout, name='logout'),
url(r'^(?P<client_name_slug>[\w\-]+)/$', views.client, name='client'),
)
# Serve media files only in development
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': 'static'}
))
else:
print "no server is configured to serve media files. Do it now."
</html>
答案 0 :(得分:2)
您应该删除urls.py中的条目。我需要提供静态文件的所有内容如下:
在我的settings.py中:
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myprojectname/static'),
)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
在我的模板中:
{% load staticfiles %}
<script type="text/javascript" src="{% static 'raphael-min.js' %}"></script>
文件的位置为./myprojectname/static/raphael-min.js
IE /home/username/djangoProjects/myprojectname/myprojectname/static/raphael-min.js