› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding asgi.py in Django
- This topic is empty.
-
AuthorPosts
-
May 13, 2026 at 2:58 pm #6573
If you create a modern Django project, you will notice a file named:
asgi.pyThis file is automatically generated by Django and acts as the entry point for ASGI-compatible web servers.
The Default
asgi.pyFile“””
ASGI config for wiki project.It exposes the ASGI callable as a module-level variable named
application.For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
“””import os
from django.core.asgi import get_asgi_application
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘wiki.settings’)
application = get_asgi_application()
What is ASGI?
ASGI stands for:
Asynchronous Server Gateway Interface
It is the modern interface between:
- your Django application
- and the web server running it
ASGI allows Django to support:
- asynchronous programming
- WebSockets
- real-time communication
- high concurrency
- live updates
Simple Flow
Browser
↓
ASGI Server
↓
asgi.py
↓
Django Project
Line-by-Line Explanation
Importing the
osModuleimport os
The
osmodule helps Python interact with:- environment variables
- operating system settings
Importing Django’s ASGI Helper
from django.core.asgi import get_asgi_application
This imports Django’s built-in helper function that creates the ASGI application object.
Setting the Django Settings Module
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘wiki.settings’)
This tells Django which settings file to use.
In this case:
wiki/settings.py
Without this line, Django would not know:
- installed apps
- database settings
- middleware
- templates
- static file configuration
Creating the Application Object
application = get_asgi_application()
This creates the ASGI application object.
The web server later imports this variable:
application
to run the Django project.
Why is the Variable Named
application?ASGI servers expect a callable named:
application
This is a standard naming convention used by servers such as:
- Uvicorn
- Daphne
- Hypercorn
Request Flow
Browser Request
↓
ASGI Server
↓
application object in asgi.py
↓
Django Project
↓
Views and URLs
↓
Response
Difference Between
wsgi.pyandasgi.pyFeature WSGI ASGI Type Synchronous Async + Sync WebSockets No Yes Real-Time Apps Limited Excellent Traditional Deployments Very Common Modern Approach
When is ASGI Useful?
ASGI becomes especially useful for applications like:
- chat applications
- live dashboards
- real-time notifications
- streaming systems
- multiplayer apps
- async APIs
Example ASGI Server Command
uvicorn wiki.asgi:application
Meaning:
wiki.asgi→ import theasgi.pyfileapplication→ use the application object
Important Insight
Modern Django projects automatically generate both:
wsgi.py
asgi.pyeven if your project does not yet use async features.
This keeps the project ready for future scalability and modern deployment methods.
In Simple Terms
asgi.pyis the bridge that allows ASGI-compatible servers to run your Django application. -
AuthorPosts
- You must be logged in to reply to this topic.
