Django 1.4静态文件找不到错误 [英] Django 1.4 static file not found error
问题描述
我刚刚进入Python和django的世界,我无法使用STATIC_URL设置我的URL,我目前正在使用django 1.4进行本地开发。我阅读了 django static file doc ,似乎添加了所有需要什么,但仍然会收到404错误。我已经阅读了其他与此相关的Q& A,并尝试了各种各样的事情,但无法使其正常工作。
I just entered the world of Python and django, i am unable to set my URL using the STATIC_URL , i am currently developing locally with django 1.4. I read the django static file doc and seem to have added all what's required, but still get a 404 error. I have read other Q&A on SO related to this, and tried all sorts of things, but could not make it work.
从控制台获取的错误
http://localhost:8000/blog/static/js/src/models/myfile.js 404 (NOT FOUND)
我的index.html页面,其他页面扩展的主页面有以下脚本: / p>
My index.html page, which is my main page that the other pages extend has the following script :
<script src="{{ STATIC_URL }}/js/src/models/myfile.js"></script>
在通过SO的其他问题和答案后,我尝试通过添加自己的archive.html来编辑我的视图正在渲染数据:
After going through other questions and answers on SO i tried editing my view by adding since my archive.html is rendering data :
from django.template import RequestContext
...
return render_to_response('archive.html', {'categories' : resultSet, 'data': queryData}, context_instance=RequestContext(request))
我的目录结构 -
mysite
|-- manage.py
|-- mysite
|-- __init__.py
|-- settings.py
|-- urls.py
|-- wsgi.py
|-- blog
|-- __init__.py
|-- models.py
|-- tests.py
|-- views.py
|-- temapltes
|-- archive.html
|-- base.html
|-- static
|-- js
|-- myfile.js
我的Settings.py
My Settings.py
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'C:/Python27/Scripts/mysite/blog/static',
# A Bit confused on the path here
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
'django.core.context_processors.static',
)
TEMPLATE_DIRS = ( )
INSTALLED_APPS = (
#..
'django.contrib.staticfiles',
'blog',
)
我想我有一切都需要,但仍然没有运气。
I think i have everything thats needed, but still no luck with it.
我哪里错了,我设置STATICFILES_DIRS路径有点困惑,我怎么可以得到它在我的本地机器上工作
Where have i gone wrong, i am a bit confused on setting the "STATICFILES_DIRS" path, how i can get this working on my local machine
推荐答案
您应该更改目录结构:
应用级别
/blog/static/blog/js
项目级别:
/static/js
在Django 1.4中也可以使用静态标签为方便起见:
Also in Django 1.4 you can use the static tag for convenience:
{% load static %}
<img src="{% static 'js/abacadabra.js' %}" />
最后但并非最不重要的是,您可能希望避免使用硬编码路径,因此请更改:
Last but not least, you probably want to avoid hard coded paths, so change:
STATICFILES_DIRS = (
'C:/Python27/Scripts/mysite/blog/static',
)
to:
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
这篇关于Django 1.4静态文件找不到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!