Cliff
Cliff

Reputation: 11268

My DJango app is responding to /static urls

Update

I figured out what was causing the stylesheets to become invisible, though I don't quite understand it all. I set DEBUG=False in settings.py to test the error handling as described in the tutorial. Somehow setting debug to false causes the static files not to be locatable. I will look further in the configs to see if I can get a clear understanding why. Until then, please feel free to answer or comment with additional info. I am still learning!

Update

I'm going through a DJango tutorial from here and I hit a roadblock. I'm up to tutorial 3 where they explain how to refactor your urls.py file when I try loading up the admin site to make sure I haven't broken it. Sure enough it looked all wierd because it was missing the stylesheets. Stylesheets are pulled from here:

http://127.0.0.1:8000/static/admin/css/base.css

When I hit that link in my browser I get the custom 404 page I configured for my app. The stylesheets were working prior but I'm not sure which change broken them. I went through my urls.py file and reverted all of the polls specific url configs to no avail. Here's my current urls.py under hellodjango (the name of my project.)

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.http import HttpResponse

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

def page_not_found(request, template_name='404.html'):
    return HttpResponse("Could not find the resource you asked for...")

handler404 = 'hellodjango.urls.page_not_found'

and here's the urls.py under my polls directory:

from django.conf.urls import patterns, url

# Uncomment the next two lines to enable the admin:

urlpatterns = patterns('polls.views',
    url(r'^$', 'index'),
    url(r'^(?P<poll_id>\d+)/$', 'detail'),
    url(r'^(?P<poll_id>\d+)/results/$', 'results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

Help?

Upvotes: 0

Views: 241

Answers (3)

SmileyChris
SmileyChris

Reputation: 10792

Glad you figured it out. Here's why it works like this.

django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn't be using django to serve static files, this only happens when DEBUG = True, as you found out.

The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Upvotes: 0

SmileyChris
SmileyChris

Reputation: 10792

The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?

INSTALLED_APPS = (
    # ...
    'django.contrib.staticfiles',
)

In production, you'll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting - it isn't used or needed during development).

Upvotes: 0

Peter
Peter

Reputation: 1359

It looks like you don't have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn't match any pattern, and so you get a 404. Try something like this:

from django.conf.urls.static import static

# ...
urlpatterns = patterns('',
# ...
                       url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
                           {'document_root': settings.STATIC_ROOT}),
# ...

This should work for you -- go to /static/foo.css, and you should see your CSS.

It's worth noting that this is discouraged in a production environment. For your tutorial app, though, it'll work.

Upvotes: 1

Related Questions