Bri6ko
Bri6ko

Reputation: 1905

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

Upvotes: 7

Views: 6004

Answers (4)

Sean
Sean

Reputation: 15961

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.

Upvotes: 10

Patrick Beeson
Patrick Beeson

Reputation: 1695

I struggled with this problem as well.

Here's how I solved it (DEBUG=True):

  1. After creating a project and several apps, I created a directory called "static" that sat alongside my app directories (same level as .manage.py).
  2. I set my static URL to '/static/' in settings
  3. I set my static root to STATIC_ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '../../static/').replace('\\','/')) (BASE_DIR = os.path.dirname(os.path.dirname(__file__)))
  4. I set my staticfiles dirs to STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), '/Users/username/sites/containing_dir/project_dir/static/',) (assumes OSX path)
  5. I added + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to my urls.py for my project

My static media is now served from http://127.0.0.1:8000/static/css/styles.css (assuming runserver command).

Comment if you need addition description!

Upvotes: 1

chrisortman
chrisortman

Reputation: 1512

I ran into this problem too. My problem was that I didn't have my main app in the installed apps, whatever folder that has wsgi.py in it, that needs to be in INSTALLED_APPS.

Upvotes: 5

Surya Kasturi
Surya Kasturi

Reputation: 4836

Firstly it has to be known that Django is not responsible for serving static files on production servers. However, it does serve them when DEBUG=True during development for the sake of debugging.

For serving static files when debug=true you have to do nothing. I don't know why it is creating confusion among newbies on how to setup it. The below should help you create static files for both production and dev modes clearly

Development mode : when DEBUG=True

  1. create your project django-admin.py startproject mysite
  2. create your app manage.py createapp myapp
  3. Add myapp to INSTALLED_APPS in settings.py
  4. In myapp dir, create a folder static
  5. In the myapp/static dir, save a file myfile.css
  6. In settings.py, provide a valid address to STATIC_ROOT folder to store static files. It could be something like os.path.join(os.path.abspath(os.path.dirname(__file__)), "mystatic") if you want to store your collected files at mysite/mysite/mystatic.
  7. run manage.py collectstatic; now you have to see the static file collected at the directory you mentioned above..It should also create one if its not present. You are free to put any directory.
  8. test the url http://127.0.0.1:8000/static/myfile.css
  9. In case if its not working yet, try restarting the site.. New files are not recognized dynamically!

Upvotes: 0

Related Questions