问题
Possible Duplicate:
Trying to serve django static files on development server - not found
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.
The error i get from the console
http://localhost:8000/blog/static/js/src/models/myfile.js 404 (NOT FOUND)
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>
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))
My directory structure -
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
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.
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
回答1:
You should change your directory structure:
app level:
/blog/static/blog/js
project level:
/static/js
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'),
)
来源:https://stackoverflow.com/questions/12094401/django-1-4-static-file-not-found-error