Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Files With Django 1.6

I have spent all day and nothing works. I have seen at least 20 posts here about the same topic and they are different with different suggestions and nothing works for me. Running Django 1.6 with Python 2.7.+ I'm trying to load the css for the polls app from the django tutorial.

project layout

Project
   |-polls
       |-static
           |-polls
                style.css
   |static

here is the settings.py

STATIC_URL = '/static/'
STATIC_ROOT = '/home/bri6ko/DjangoProjects/django1.6/PoolsDjangoProject/static'

template code

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">

urls.py

urlpatterns = patterns(...)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

polls/urls.py

urlpatterns = patterns(....,url(r'^static/(?P<path>.*)$', 'django.views.static.serve',                                        {'document_root': '/home/bri6ko/DjangoProjects/django1.6/PoolsDjangoProject/'}),)

After running: python manage.py collectstatic, everything is collected and static folder which is on root gets populated accordingly with the admin and polls folders and files.

I start the server: python manage.py runserver everything works fine except the css is not loaded

this is what i get from the Chrome console when i inspect the html

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

which looks fine but gives the following output

[16/Nov/2013 00:44:41] "GET / HTTP/1.1" 404 2141
[16/Nov/2013 00:44:45] "GET /polls/ HTTP/1.1" 200 820
[16/Nov/2013 00:44:45] "GET /static/polls/style.css HTTP/1.1" 404 1649

Any help would be appreciated. I followed everything step by step. I don't know what to do anymore

UPDATE

DEBUG=True

full urls.py

from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from PoolsDjangoProject import settings
from django.conf.urls.static import static

admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'PoolsDjangoProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

full polls/urls.py

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

urlpatterns = patterns('',
                   url(r'^$', views.IndexView.as_view(), name='index'),
                   url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
                   url(r'^(?P<pk>\d+)/results/$',     views.ResultsView.as_view(),name='results'),
                   url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote', name='votes'),
                   url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
                                    {'document_root':       '/home/bri6ko/DjangoProjects/django1.6/PoolsDjangoProject/'}),
)

And when i try to access

http://127.0.0.1:8000/static/polls/style.css

I get

Page not found (404)
'polls/style.css' could not be found
like image 955
Bri6ko Avatar asked Nov 16 '13 00:11

Bri6ko


People also ask

Does Django serve static files?

For local development the Django web server automatically serves static files and minimal configuration is required. A single Django project often contains multiple apps and by default Django will look within each app for a static directory containing static files.

Where should I put static files in Django?

Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

How does Django find static files?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

What are Django static files?

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.


1 Answers

I found it necessary to restart the running server after following the steps in

https://docs.djangoproject.com/en/1.6/intro/tutorial06/#customize-your-app-s-look-and-feel

in order to pick up the newly created static directory. That step isn't listed in the tutorial. I have opened a ticket requesting its addition.

I realise this answer is not likely to help your specific case but this question comes up prominently in Google when searching for problems with adding a static directory to the example Polls app.

like image 51
Sean Avatar answered Sep 28 '22 01:09

Sean