pythondjangoiis-8.5

Static Files from Site working, but not from app


Some confusion about static files (Python 3.6 Django 2.. IIS 8.5) My static files from site are found but the static files from the app are not found.

GET http://127.0.0.1:81/static/css/pyapp.css 404 (Not Found)

<link rel="stylesheet" type="text/css" href="{% static 'css/pyapp.css' %}">

I tried to add

STATICFILES_DIRS =[(os.path.join(BASE_DIR, 'pyapp/static')),]"<

to the settings.py but did not help.

Do I have to add a web.config also in the app static like in the site static folder?

├───pyapp
│   ├───migrations
│   │   └───__pycache__
│   ├───static
│   │   └───css
|   │   │   └───pyapp.css   <=== this is not found
│   ├───templates
│   │   └───pyapp
|   │   │   └───my.html  <=== this works
│   └───__pycache__
└───pyweb
    ├───static           <=== this works
    │   └───admin
    │   ......
    └───__pycache__

For IIS configuration i used this tutorial: http://blog.mattwoodward.com/2016/07/running-django-application-on-windows.html

settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '...'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'pyapp.apps.PyappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'pyweb.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'pyweb.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'de-ch'

TIME_ZONE = 'Europe/Berlin'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATICFILES_FINDERS = (
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS =[(os.path.join(BASE_DIR, 'pyapp/static')),]

Project structure


Solution

  • I found the Problem. Thx for help!

    I don't understand why IIS is not refreshing the VirtualDirectory. I refreshed x times the website and restarted it. But the pyapp folder in static was not visible until i set the physical path again.

    sites
    ├───pyweb
    |    └───pyapp
    |    └───pyweb
    |    └───static
    |    |    └───admin
    |    |    └───pyapp   <=== this was not visible in IIS, refreshing not enough!
    

    Is this a bug or should I know this?