找不到django 1.6中的静态文件

时间:2014-05-02 09:43:32

标签: django static django-staticfiles django-1.6

我刚开始使用django 1.6并且我的css不能同时处理我的图像,我收到404错误,即使是管理员(s css无效)

这是我的文件结构:

myproject
--myproject
----templates
--static
----css
----media
--myapp
--mp.db
--manage.py

这是我的base.html:

<link rel="image_src" href="{% block image_src %}http://{{ request.META.HTTP_HOST }}/static/images/logo.png{% endblock %}" />
<link rel="stylesheet" media="screen" href="{{ STATIC_URL }}css/utils.css">
<link rel="stylesheet" media="screen" href="{{ STATIC_URL }}css/{% block style_link %}style{% endblock %}.css">
   ...
<div id="cont_footer" class="container centerAuto">
<img src="{{ MEDIA_URL }}images/logo_footer.png" alt="" />
</div>

这是我的设置:

PROJECT_ROOT = os.path.dirname( __file__ )
PROJECT_NAME = os.path.basename(PROJECT_ROOT)

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static/'),
)

STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder',
     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.static',
)
MEDIA_URL = STATIC_URL +'media/'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))

在urls.py中:

urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='index.html'), name="index"),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我做错了什么?因为这三天以来我已经脱掉了头发!!

由于

1 个答案:

答案 0 :(得分:0)

制作文件夹&#34;静态&#34;在您的项目的根目录中,settings.py存在

在settings.py中

STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
                os.path.join(PROJECT_DIR,'static'),
)
TEMPLATE_DIRS = (
                os.path.join(PROJECT_DIR,'template'),
)

在base.html中

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

<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>

在其中包含base.html

的其他模板文件中
{% extends "base.html" %}
{% load static %}


<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>


<div id="yourID" class="yourClass">
    <img src="{% static "images/something.gif" %}" alt="something" >
</div>

urls.py

from django.conf.urls import patterns, include, url
from wepl.views import home
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from myApp import settings
from django.views.generic.base import TemplateView
from views import *

urlpatterns = patterns('',
    url(r'^$', 'myApp.views.home', name='home'),

    )

urlpatterns += staticfiles_urlpatterns()