'Problems with static files django, IIS 10 and windows 10

Problems with static files django, IIS and windows 10

Hi I have Python 3.10.4, Django 4.0.4 and wfastcgi 3.0.0 installed. This on windows server 2016 and the latest version of IIS as of 05/20/2022. I have already run the command manage.py runserver collectstatic. Does not show static files when viewing the web application on the internet.

This file is web.config which is outside the project folder.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="Python FastCGI"
        path="*" 
        verb="*"
        modules="FastCgiModule" 
        scriptProcessor="C:\Python35\python.exe|C:\Python35\lib\site-packages\wfastcgi.py" 
        resourceType="Unspecified" />

      <add name="Proyectoqr" 
        path="*" 
        verb="*" 
        modules="FastCgiModule"
        scriptProcessor="C:\Python35\python.exe|C:\Python35\lib\site-packages\wfastcgi.py" 
        resourceType="Unspecified" 
        requireAccess="Script" />
      
      <add name="PythonHandler" 
        path="*" 
        verb="*" 
        modules="FastCgiModule" 
        scriptProcessor="C:\Python35\python.exe|C:\Python35\lib\site-packages\wfastcgi.py" 
        resourceType="Unspecified"
        requireAccess="Script" />
    </handlers>
  </system.webServer>

  <appSettings>
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\proyectoqr" />
    <add key="WSGI_HANDLER" value="proyectoqr.wsgi.application" />
    <add key="DJANGO_SETTINGS_MODULE" value="proyectoqr.settings" />
  </appSettings>
</configuration>  

This is the content of the web.config file in the static folder

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <clear/>
            <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read"/>
        </handlers>
    </system.webServer>
</configuration>

I already added the virtual directory of the static folder with direction to the static folder of the project as shown in the following image.

enter image description here

If you need more information tell me to show you the code or configuration

Achieve settings.py

import os
from pathlib import Path
from telnetlib import LOGOUT
from django.urls import reverse_lazy

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-qqhlm_^kgh#ri8^c_5)3_-vn@w(b_2t&v%!2o*d(xli6%&-pgg'

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

ALLOWED_HOSTS = ['10.100.10.133', 'localhost', "127.0.0.1:8000", '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    'import_export',

    "apps.equipo",
    "apps.usuariof",
]

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 = 'proyectoqr.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["templates"],
        
        '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 = 'proyectoqr.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.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/4.0/topics/i18n/

LANGUAGE_CODE = 'es-mx'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

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

STATIC_URL = ''

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
#STATICFILES_DIRS = (BASE_DIR,'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

LOGIN_URL = 'login'
LOGOUT_URL = 'logout'

enter image description here

This is how I create the url's in python

                    <li class="menu-item-has-children dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="menu-icon fa fa-book"></i>Historial de prestamos</a>
                        <ul class="sub-menu children dropdown-menu">                           
                             <li><i class="fa fa-table"></i><a href="{% url 'equipo:listar_prestamos' %}">Listado de prestamos</a></li>                    
                             <li><i class="fa fa-table"></i><a href="{% url 'equipo:listar_prestamos_no_devueltos' %}">Listado de prestamos en curso</a></li>
                             <li><i class="fa fa-table"></i><a href="{% url 'equipo:listar_prestamos_devueltos' %}">Listado de prestamos terminados</a></li>
                         </ul>
 
                     </li>

This is my urls.py file which is located in the project folder

from django.contrib import admin
#from django.conf.urls import url
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

from django.contrib.auth import login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import LoginView, logout_then_login
from apps.equipo.views import home, Inicio
from apps.usuariof.views import Login, logoutUsuario

urlpatterns = [
    path('admin/', admin.site.urls),
    path("equipo/", include(("apps.equipo.urls","equipo"))),
    path("index", login_required(Inicio.as_view()), name="index"),
    path("test", Inicio.as_view(), name="test"),
    path('accounts/login/', Login.as_view(), name='login'),
    path("logout/", login_required(logoutUsuario), name="logout")
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source