1

I am unable to get my static files to work. I've spent 6 hours looking at the many posts on this topic but I must still be doing something wrong. Please help. Here are my settings:

projectfiles
|
|-----myproject
|     |
|     |-----static
|     |     |
|     |     |-----css
|     |     |-----js
|     |-----__init__.py
|     |-----settings.py
|     |-----urls.py
|     |-----wsgi.py
|
|-----myapp
|
|-----templates





settings.py
import os
SITE_ROOT = (os.path.realpath(os.path.dirname(__file__))).replace('\\','/')
DEBUG = True
MEDIA_ROOT = (os.path.join(SITE_ROOT, '/static')).replace('\\','/')
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = ''
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )





urls.py
urlpatterns = patterns('',
    (r'^myurl/$', myview),
)
from myproject.settings import DEBUG
if DEBUG:
    urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve',

                                 {'document_root': 'static'}))





mytemplate.html
...
<head>
<link src="css/mycss.css" rel="stylesheet" type="text/css"/>
...

the app works fine but no connection to my css's or javascripts. What am I misssing?

Any help would be sooo greatly appreciated.

Update:

`STATIC_ROOT='C:/path/to/myproject/static/' 
STATIC_URL='/static/' 
TEMPLATE_CONTEXT_PROCESSORS=('...static', ) 
STATICFILES_DIRS=('C:/absolute/path/to/myapp/static',) 
STATICFILES_FINDERS=('...FileSystemFinder','...AppDirectoriesFinder',) 
INSTALLED_APPS = (...,'django.contrib.staticfiles',) 

#does not work with or without this: 
urlpatterns += staticfiles_urlpatterns() 
#views now rendered like this: 
myview(request): 
... 
    return render_to_response('template.html',{'a': a},context_instance =RequestContext(request)) 


#template.html 
<link src="{{STATIC_URL}}css/mycss.css"/>
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
sammy
  • 87
  • 6

3 Answers3

3

MEDIA_ROOT AND MEDIA_URL are not used for serving static content with advent of the staticfiles app from django 1.3 so, I suggest using STATIC_URL and STATIC_ROOT instead to configure the static files.

#settings.py

STATIC_ROOT = "Absolute path to your static dir"
STATIC_URL  = "/static/"

#views:Make sure to pass RequestContext to the template.

def view_to_display_the_page(request)
    ...
    return render_to_response("templae.html", context_instance = template.RequestContext(request))

#template:

<script src="{{STATIC_URL}}path_your_static_file_relative_to_static_dir"></script>

#urls.py: Make sure to add url patterns for static file
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
  • Thanks Raunak. questions: do I write a load_statics view for each template? – sammy Nov 29 '12 at 18:58
  • @sammy: This is just an example. The point is whenever you want to serve static content your view needs to pass the RequestContext to the template to serve the static file – Raunak Agarwal Nov 29 '12 at 19:02
  • could you please elaborate on `load_statics`. Not quite sure I understand how to use it. – sammy Dec 01 '12 at 08:38
  • Don't confuse yourself because of the load_statics view it just denotes the view function which you would be using to render the template. You are required to pass the RequestContext to your template. Let me know if you have any other question – Raunak Agarwal Dec 01 '12 at 19:24
  • here's the latest. Thanks for helping me out @Raunak. I;m dying over here. – sammy Dec 02 '12 at 05:32
  • `STATIC_ROOT='C:/path/to/myproject/static/' STATIC_URL='/static/' TEMPLATE_CONTEXT_PROCESSORS=('...static', ) STATICFILES_DIRS=('C:/absolute/path/to/myapp/static',) STATICFILES_FINDERS=('...FileSystemFinder','...AppDirectoriesFinder',) INSTALLED_APPS = (...,'django.contrib.staticfiles',) #does not work with or without this: urlpatterns += staticfiles_urlpatterns() #views now rendered like this: myview(request): ... return render_to_response('template.html',{'a': a},context_instance =RequestContext(request)) #template.html – sammy Dec 02 '12 at 05:52
  • Everything looks ok to me. What error do you receive when you try to access the file. – Raunak Agarwal Dec 02 '12 at 07:53
  • No error whatsoever. The app works as designed but no CSS or JavaScript. – sammy Dec 02 '12 at 08:18
  • Thanks so much for all your help. I finally found the error. I had somehow changed my PyCharm settings to prod and was running in dev. Total bone-headed thing. Still I learned the correct way to use static files in django so it was worth it. Thanks again to everyone. – sammy Dec 09 '12 at 01:19
0
# RTFD: https://docs.djangoproject.com/en/dev/howto/static-files/

# Points to a directory for storing file uploads. Should be located outside of your package sources, i.e. your 
MEDIA_ROOT = os.path.join(SITE_ROOT, '..', 'media')
MEDIA_URL = '/media/'

# Points to the directory your static asset sources get
# collected/precompiled for distribution. Should be located outside 
# of your package sources. If STATICFILES_DIRS includes STATIC_ROOT, 
# an exception is raised.
STATIC_ROOT = os.path.join(SITE_ROOT, '..', 'static')
STATIC_URL = '/static/'

# Can be left empty if your static sources are on paths discoverable by
# STATICFILES_FINDERS. This is not true in your case, so configure the path.
STATICFILES_DIRS = [os.path.join(SITE_ROOT, 'static')]

# Use this to get the STATIC_URL path as a context variable in your templates
TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    'django.core.context_processors.static',
)

<!--And make use of it-->
<script src="{{STATIC_URL}}js/lib/jquery.js"></script>

# Run this as a test preflight to see if you've configured the settings correctly
# $ python manage.py --collectstatic
#
# Make sure you don't get any errors.

# Make sure you've hooked the static files app URLs into your main URL modules patterns
from django.conf import settings
from django.conf.urls import staticfiles_urlpatterns, static

urlpatterns = (patterns('',
    # ...
) + staticfiles_urlpatterns()
  + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
0

Using Django version 1.6; STATIC_URL cannot be a stem; Not sure when this changed.

The following type of specification in settings.py gives a 404:

STATIC_URL = '/static/'

This works:

STATIC_URL = 'http://localhost:8000/static/'
toppur
  • 1,606
  • 13
  • 12
  • The Django docs disagree (by telling you to use "STATIC_URL = '/static/'"): https://docs.djangoproject.com/en/1.7/howto/static-files/ – dantiston Feb 18 '15 at 06:53