@Blackman, Sam -- Looking at the logs, it looks like your requirements.txt wasn't installed. Normally there's log output mentioning that App Service is installing your packages. Can you try adding an app setting, SCM_DO_BUILD_DURING_DEPLOYMENT with a value of 1 and redeploy your code? Also, what tool are you using to deploy your code (VS Code extension, CLI, GitHub Actions, etc.)?
Application Error Loading 'Successfully Deployed' Web App (Deploying Django app via Azure)
I recently added my first static files for my sjango App.
Everything works when I perform py manage.py runserver to test the app locally.
However, I get an Application Error page when running the cloud page it ought to be on.
During set-up, I tried to follow the advice at the following sites to serve them via my app service:
- https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#serve-static-files-for-django-apps
- https://docs.djangoproject.com/en/3.1/howto/static-files/
- https://docs.djangoproject.com/en/3.1/howto/static-files/deployment/
The result of this looks like the following:
(project/project/settings.py)
INSTALLED_APPS = [ #... other installed apps ...
"whitenoise.runserver_nostatic", # To ease serving static files via Azure
]
MIDDLEWARE = [ #... other middleware ...
'whitenoise.middleware.WhiteNoiseMiddleware',
]
# ... other code between these parts ...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = os.environ.get("DJANGO_STATIC_URL", "/static/")
STATIC_ROOT = os.environ.get("DJANGO_STATIC_ROOT", "./static/")
STATICFILES_STORAGE = ('whitenoise.storage.CompressedManifestStaticFilesStorage')
(project/app/views.py)
# ... other, non-landing-page views
def index(request):
""" View function for site landing page"""
return render(request, 'index.html')
(project/project/urls.py)
from django.contrib import admin
from django.urls import path
from django.urls import include
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]
# Use include() to add paths from the interface application
urlpatterns += [
path('interface/', include('interface.urls')),
]
# Add URL maps to redirect the base URL to my application
urlpatterns += [
path('', RedirectView.as_view(url='interface/', permanent=True)),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
(project/app/urls.py)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
(project/app/templates/index.html)
{% extends "base_temp.html" %}
{% block content %}
<h1>App Home</h1>
<p>a website developed by <em>Me</em>!</p>
{% endblock %}
(project/app/static/css/styles.css)
.sidebar-nav {
margin-top: 20px;
padding: 0;
list-style: none;
}
While deployment is 'successful', I noticed the following things in my output, which also appear in my deployment logs:
Content in source directory is a Django app
11:23:49 AM project: Running collectstatic...
11:23:50 AM project: Traceback (most recent call last):
11:23:50 AM project: File "manage.py", line 24, in <module>
11:23:50 AM project: main()
11:23:50 AM project: File "manage.py", line 20, in main
11:23:50 AM project: execute_from_command_line(sys.argv)
11:23:50 AM project: File "/tmp/8d9e7fab0f97fc8/antenv/lib/python3.8/site-packages/django/core/management/init.py", line 425, in execute_from_command_line
11:23:50 AM project: utility.execute()
11:23:50 AM project: File "/tmp/8d9e7fab0f97fc8/antenv/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute
11:23:50 AM project: django.setup()
11:23:50 AM project: 'collectstatic' exited with exit code 1.
11:23:50 AM project: File "/tmp/8d9e7fab0f97fc8/antenv/lib/python3.8/site-packages/django/init.py", line 24, in setup
11:23:50 AM project: Done in 1 sec(s).
11:23:50 AM project: Not a vso image, so not writing build commands
11:23:50 AM project: Preparing output...
11:23:50 AM project: apps.populate(settings.INSTALLED_APPS)
11:23:50 AM project: File "/tmp/8d9e7fab0f97fc8/antenv/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
11:23:50 AM project: app_config.import_models()
11:23:50 AM project: File "/tmp/8d9e7fab0f97fc8/antenv/lib/python3.8/site-packages/django/apps/config.py", line 300, in import_models
11:23:50 AM project: self.models_module = import_module(models_module_name)
11:23:50 AM project: File "/tmp/oryx/platforms/python/3.8.12/lib/python3.8/importlib/init.py", line 127, in import_module
11:23:50 AM project: return _bootstrap._gcd_import(name[level:], package, level)
11:23:50 AM project: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
11:23:50 AM project: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
11:23:50 AM project: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
11:23:50 AM project: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
11:23:50 AM project: File "<frozen importlib._bootstrap_external>", line 843, in exec_module
11:23:50 AM prject: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
11:23:50 AM project: File "/tmp/8d9e7fab0f97fc8/interface/models.py", line 1, in <module>
11:23:50 AM project: from asyncio.windows_events import NULL
11:23:50 AM project: File "/tmp/oryx/platforms/python/3.8.12/lib/python3.8/asyncio/windows_events.py", line 3, in <module>
11:23:50 AM project: import _overlapped
11:23:50 AM project: ModuleNotFoundError: No module named '_overlapped'
which was out of the ordinary for previous successful deployments (before I added statics).
I found a suggestion online to run collect statics BEFORE deployment, to solve the collect statics returning code 1. I did so, collectstatics ran successfully, but I am still getting identical output.
Any suggestions on why this is running locally but not when I deploy it to Azure, and how I can fix it?