Search code examples
djangostatic-filesdjango-staticfiles

Serving django static files in development envirnoment


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...


Solution

  • 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',
        ...
        ...
    )