pythondjangoazureazure-web-app-service

AttributeError: module 'django.conf.global_settings' has no attribute 'ROOT_URLCONF' no solutions found worked


No matter what I do, I get this error in Azure Stream logs, everything works fine when I run the project locally.

my wsgi.py file:

import os

from django.core.wsgi import get_wsgi_application


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mojspecjalista.settings')

application = get_wsgi_application()

My project url file:

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('', include('App.api.urls')),
    path('admin/', admin.site.urls),
]

manage.py:

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mojspecjalista.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

and lastly my settings:

ROOT_URLCONF = 'mojspecjalista.urls'

Solution

  • Ensure your wsgi.py file correctly sets the DJANGO_SETTINGS_MODULE to 'mojspecjalista.settings', and ensure your project structure is consistent. Double-check that all necessary files and configurations are deployed and restart the server if needed. Make sure your packages are up to date.

    ROOT_URLCONF is correctly referred to according to your project structure.

    I tried your code with minor changes, and I successfully deployed the Python Django app to Azure App Service.

    manage .py

    import os
    import sys
    
    def main():
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mydjango.settings')
        try:
            from django.core.management import execute_from_command_line
        except ImportError as exc:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            ) from exc
        execute_from_command_line(sys.argv)
    
    if __name__ == '__main__':
        main()
    

    This is my mydjango/urls.py:

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('', include('myapp.urls')),
        path('admin/', admin.site.urls),
    ]
    if  settings.DEBUG:
    
    urlpatterns  +=  static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    This is the content of my settings .py file:

    import os
    from pathlib import Path
    
    BASE_DIR = Path(__file__).resolve().parent.parent
    
    SECRET_KEY = 'secretekey'
    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True
    ALLOWED_HOSTS =  ['yourazurewebappname.azurewebsites.net', 'yourdomain.azurewebsites.net', '127.0.0.1', 'localhost']
    
    INSTALLED_APPS = [
        '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 = 'mydjango.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 = 'mydjango.wsgi.application'
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
    
    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',
        },
    ]
    
    LANGUAGE_CODE = 'en-us'
    TIME_ZONE = 'UTC'
    USE_I18N = True
    USE_TZ = True
    
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
    

    myapp/views.py

    from  django.http  import  HttpResponse
    
    def  my_view(request):
    
    return  HttpResponse("Hello, Django!")
    

    myapp/urls.py

    from django.urls import path
    from .views import my_view  # Import your view function
    urlpatterns = [
        path('', my_view, name='my-view'),
        # Add other URL patterns for your app as needed
    ]
    

    myapp/models.py

    from django.db import models
    
    class MyModel(models.Model):
        field1 = models.CharField(max_length=100)
        field2 = models.IntegerField()
    

    Local Output : enter image description here

    enter image description here

    enter image description here

    I deployed the code to the Azure App Service using the VS Code Azure Extension.

    enter image description here

    The deployment to Azure App Service was successful.

    enter image description here

    Azure Output : enter image description here

    enter image description here