0

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

Community
  • 1
  • 1
shabeer90
  • 5,161
  • 4
  • 47
  • 65
  • 1
    Extended settings example: http://stackoverflow.com/questions/11033571/using-static-files-with-the-django-virtual-server/11033706#11033706 – Hedde van der Heide Aug 23 '12 at 15:08
  • http://stackoverflow.com/questions/9197599/how-to-serve-static-files-in-django-development-server – supervacuo Aug 23 '12 at 15:32
  • http://stackoverflow.com/questions/12026010/trying-to-serve-django-static-files-on-development-server-not-found/12029220 – supervacuo Aug 23 '12 at 15:33
  • -1 with a heavy heart. It's not a bad question, but it's been asked countless times, and any of the answers would have solved your issue. – supervacuo Aug 23 '12 at 15:34

1 Answers1

3

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'),
)
Raphael Ahrens
  • 953
  • 15
  • 29
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • thanks alot , after posting my question i came across the Django Static Template tags, works fine for me :D – shabeer90 Aug 23 '12 at 15:12