0

I want to use static files to load my css with runserver command. The problem is that i tried all the solution i found here on stackoverflow and also in django docs, but it doesnt works at all... I dont know what can i do... If i set

STATIC_URL = '/static/'
STATIC_ROOT = 'C:\Users\Max\Works\www\mysite\mysite\static'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

I thought it was enough... Am i missing something? Can you tell me what is the best setting for have static files in develompent envirnoment? Thanks in advice...

EDIT(1) I already putted in my template {{ STATIC_URL }}css/dark-grey.css" and ofc the css is in C:\Users\Max\Works\www\mysite\mysite\static\css\dark-grey.css, i really can't get what is wrong...

Maksim
  • 215
  • 2
  • 12

4 Answers4

1

Use / slashes and NOT \ slashes in the path, even for windows paths.

greenafrican
  • 2,516
  • 5
  • 27
  • 38
0

You need to add url patterns:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of your URLconf here ...

urlpatterns += staticfiles_urlpatterns()

See the documentation here

c4urself
  • 4,207
  • 21
  • 32
  • Did it and i get same error, Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/static/css/dark-grey.css – Maksim Dec 23 '11 at 17:04
  • hmm ok. let's see does it have to do with your windows path in static_url? can you check it with an appended /? – c4urself Dec 23 '11 at 17:09
  • Sorry, i meant append a '/' to the end of your static_root setting – c4urself Dec 23 '11 at 17:17
  • You dont need to add this if you run with DEBUG=True. See my answer – jdi Dec 23 '11 at 17:20
  • Acctually i fixed this part with jdi suggestion, but i still have problems with context processor... – Maksim Dec 23 '11 at 17:36
0

In your settings.py

DEBUG=True

As per the docs:

This view is automatically enabled by runserver (with a DEBUG setting set to True).

Using the URL pattern is a way to force it, which I personally don't even have to do in my project as long as DEBUG=True. You would always have DEBUG on when you are developing, and when you switch to production you aren't even using the development server anyways, so you would be pointing your production server to the static location.

This is a snippet of my static settings from my settings.py. I do not manually have to add that static view URL

import os

DEBUG = True

PROJECT_ROOT = os.path.dirname( __file__ )
PROJECT_NAME = os.path.basename(PROJECT_ROOT)

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, 'web/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)


TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.static',
    ...
    ...
)
jdi
  • 90,542
  • 19
  • 167
  • 203
  • Tried it, but it doesnt work at all. I copy/paste your code to my settings.py and it gives me 404 error as before... – Maksim Dec 23 '11 at 17:21
  • Just copying and pasting wouldnt fix it instantly. Did you modify the STATICFILES_DIRS to point to a directory that has your static content? Mine is called web/ right under the project root. That is where I keep all my top level general static content. If apps have their own there would be a static/ dir under each app. The STATIC_ROOT is not important for this current situation actually. – jdi Dec 23 '11 at 17:24
  • Yea i got it, now it works, thanks a lot... Now i have the problem that my template doesnt replace correctly the path of files... href="css/dark-grey.css" also if i putted in source href="{{ STATIC_URL }}css/dark-grey.css" – Maksim Dec 23 '11 at 17:29
  • @MaksimSinik - Add this to your TEMPLATE_CONTEXT_PROCESSORS: 'django.core.context_processors.static', otherwise it wont parse that tag – jdi Dec 23 '11 at 17:29
  • I dont even have TEMPLATE_CONTEXT_PROCESSORS in my settings.py, it is that a normal behaviour? – Maksim Dec 23 '11 at 17:32
  • I putted TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.static', ) to my settings.py and it still doesnt works... If i hardcode it with "/static/css/dark-grey.css" it works like a charm... – Maksim Dec 23 '11 at 17:35
  • /static/ will totally work right away. Yes the template context processors look like this: https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-in-templates – jdi Dec 23 '11 at 17:36
  • Hmm so i discovered i was using render_to_response shortcut, now i imported and used 3rd argument context_instance=RequestContext(request) and it works like a charm... Thanks finally i get it to work... This wuold be last question i'll ask for this topic. So for get all the stuff work, i have to set a dir for static files in dev mode, set static files for production, then before i publish the site run manage.py collectstatic to put all the static in the same folder and then finally i'll have all the stuff working? Am i missing something? – Maksim Dec 23 '11 at 17:47
  • @MaksimSinik - Good point on the RequestContent(). Yes so in a nutshell: 1) STATIC_ROOT points to where static WILL collect to 2) STATICFILES_DIRS are extra locations in addition to /static source locations. And in development server it will use those locations directly. 3) When using a production server like apache, use python manage.py collectstatic to collect all those static locations into your STATIC_ROOT and point apache (or other) to that as a static media location – jdi Dec 23 '11 at 17:56
  • Thanks a lot! Now i am a happy django noob! :D – Maksim Dec 23 '11 at 18:03
-3

You also need to run the following command to get the static files moved into the right place (and for Django to know they're there):

python manage.py collectstatic

Full documentation on static files in Django 1.3 is here: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

Kirsten Jones
  • 2,681
  • 1
  • 17
  • 20
  • This is only needed for when you will run a production server and need to point at a static location. In development server, it will check your static defined directories and static/ directories of the apps – jdi Dec 23 '11 at 17:06
  • Actually i already did it, and now my /static/ folder is full of file and also inside it i got another /static/ with my original css folder and files... i can access css at http://127.0.0.1:8000/static/static/css/dark-grey.css O_o – Maksim Dec 23 '11 at 17:14
  • @MaksimSinik - You actually shouldn't put anything IN the STATIC_ROOT. That is where it will go when u collect it using this collectstatic command and where you point your production server at. When you are development server mode, it will discover the static content for you in static/ subdirs of your apps, and in the extra directories you specify in the STATICFILES_DIRS (see my answer) – jdi Dec 23 '11 at 17:18
  • Thanks god! I lost 4 hours this afternoon to fix this... I read 2000 times docs and stuff and couldn't get it... I putted now the css folder in my blog app, and puff it works... Now i can see it with direct link, but i cant include it in my template with {{ STATIC_URL }}css/dark-grey.css any suggestions? – Maksim Dec 23 '11 at 17:24
  • @MaksimSinik - Add this to your TEMPLATE_CONTEXT_PROCESSORS: 'django.core.context_processors.static', otherwise it wont parse that tag – jdi Dec 23 '11 at 17:29