1

After reading more about common Django project structure, I renamed my 'media' folder to 'static' since it contains the CSS, javascript, and images for my web app and no user uploaded files. However, my project isn't finding the new directory, and the favicon is still being served and can be downloaded from the old directory at localhost:8000/media/images/favicon.png!

In my template, I am linking directly to css/js files like so:

<link href="/static/css/map.css" type="text/css" rel="stylesheet" />

There's also nothing interesting going on in urls.py:

urlpatterns = patterns('',

    url(r'^$', 'EventMapperApp.views.map', name='map'),

    # AJAX
    url(r'^all_events/', 'EventMapperApp.views.all_events'),
    url(r'^user_created_events/', 'EventMapperApp.views.user_created_events'),
    url(r'^all_user_events/', 'EventMapperApp.views.all_user_events'),

    # event actions
    url(r'^save_event/', 'EventMapperApp.views.save_event'),
    url(r'^unsave_event/', 'EventMapperApp.views.unsave_event'),
)

Any ideas?


EDIT


For some reason, I thought that urls.py didn't have to resolve all URLs for my Django app? Not really sure what I was thinking... Anyway, I've made what I thought were the appropriate changes to my code as per the documentation, but I'm still having some trouble getting it working.

settings.py

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = (os.path.join(PROJECT_PATH, "media"))

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = (os.path.join(PROJECT_PATH, "static"))

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

urls.py

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'EventMapperApp.views.map', name='map'),

    # Required to make static serving work
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

    # AJAX
    url(r'^all_events/', 'EventMapperApp.views.all_events'),
    url(r'^user_created_events/', 'EventMapperApp.views.user_created_events'),
    url(r'^all_user_events/', 'EventMapperApp.views.all_user_events'),

    # event actions
    url(r'^save_event/', 'EventMapperApp.views.save_event'),
    url(r'^unsave_event/', 'EventMapperApp.views.unsave_event'),
)

template

<link rel="icon"
      type="image/png"
      href="{{ STATIC_URL }}images/WWWfavicon.png" />
<link href="{{ STATIC_URL }}css/map.css" type="text/css" rel="stylesheet" />
<link href="{{ STATIC_URL }}css/ui-lightness/jquery-ui-1.8.21.custom.css" type="text/css" rel="stylesheet" />
<link href="{{ STATIC_URL }}css/timepicker.css" type="text/css" rel="stylesheet" />

I'm not sure what I'm doing wrong now... STATIC_ROOT seems to be resolving properly when I walk through the code (the 'static' folder is in the same directory as settings.py).


EDIT 2


As per https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development, I changed urls.py to the following:

from django.conf.urls import patterns, url
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'EventMapperApp.views.map', name='map'),

    # AJAX
    url(r'^all_events/', 'EventMapperApp.views.all_events'),
    url(r'^user_created_events/', 'EventMapperApp.views.user_created_events'),
    url(r'^all_user_events/', 'EventMapperApp.views.all_user_events'),

    # event actions
    url(r'^save_event/', 'EventMapperApp.views.save_event'),
    url(r'^unsave_event/', 'EventMapperApp.views.unsave_event'),
)

# only in development
urlpatterns += staticfiles_urlpatterns()

Still no luck.

pcaisse
  • 754
  • 2
  • 11
  • 19
  • who serve your statics file? django? so where is your url code? Managing static files | Django documentation | Django -> https://docs.djangoproject.com/en/dev/howto/static-files/ – Mohammad Efazati Jul 23 '12 at 17:48
  • Could you post your configuration file too? A lot of things related to static serving of files happen there. – Laurent Bourgault-Roy Jul 23 '12 at 17:48
  • Stop and restart your webserver, and make sure you clear any caches -- a quick way with memcached is to just restart it. – Chris Pratt Jul 23 '12 at 18:00
  • Since there's nothing in that urls.py that deals with static files at all, presumably you're serving them some other way. What way? – Daniel Roseman Jul 23 '12 at 18:04
  • Nothing is serving my static files at the moment. They're just referenced directly. Without being able to even see the files I can't set Django up to serve them! – pcaisse Jul 23 '12 at 19:07
  • I'm using the built in development web serve that comes with Django. I already restarted the server. Should I do something else? – pcaisse Jul 23 '12 at 19:11

1 Answers1

0

See here: Static files in templates & Serving static files in development

slackjake
  • 197
  • 1
  • 5