› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › What Happens When You Run python manage.py runserver?
- This topic is empty.
-
AuthorPosts
-
June 21, 2026 at 5:59 am #6969
Most Django beginners quickly learn to start a project with:
python manage.py runserverThe command starts the development server, and suddenly the website becomes available in the browser.
But what actually happens behind the scenes?
Let’s follow the complete journey step by step.
The Command
When you type:
python manage.py runserverPython executes the
manage.pyfile.At this moment:
sys.argvcontains:
[ "manage.py", "runserver" ]The word
runserveris passed to Django as a command.
Step 1: Python Starts
manage.pyThe first file executed is:
manage.pyInside that file:
if __name__ == "__main__": main()Since the file is being run directly, Python calls:
main()
Step 2: Django Settings Are Loaded
Inside
main():os.environ.setdefault( "DJANGO_SETTINGS_MODULE", "wiki.settings" )This tells Django:
Use the settings found in
wiki/settings.py.Without this information Django would not know:
- Which database to use
- Which apps are installed
- Which middleware to load
- Which templates directory exists
Step 3: Django Imports the Command Engine
Next:
from django.core.management import execute_from_command_lineThis imports Django’s management system.
Think of it as Django’s command dispatcher.
It knows how to process commands such as:
runserver migrate makemigrations createsuperuser shell
Step 4: Django Reads the Command
This line executes:
execute_from_command_line(sys.argv)Django receives:
[ "manage.py", "runserver" ]and recognizes:
The requested command is
runserver.Now Django begins preparing the application.
Step 5: Django Loads
settings.pyDjango imports:
wiki/settings.pyThis file contains project configuration.
Typical examples include:
INSTALLED_APPSDATABASESMIDDLEWARETEMPLATESSTATIC_URLDjango now understands how the project is configured.
Step 6: Django Loads Installed Apps
Suppose:
INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "encyclopedia" ]Django imports each app.
For example:
encyclopedia/is loaded and registered.
This allows Django to discover:
- Models
- Views
- Admin configurations
- App settings
Step 7: Django Loads Middleware
Next Django loads middleware.
Example:
MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", ]Middleware sits between:
Browser ↓ Middleware ↓ ViewsEvery request and response passes through middleware.
Step 8: Django Checks for Problems
Before starting the server Django performs a system check.
Equivalent to:
python manage.py checkIt looks for common issues such as:
- Invalid settings
- Missing apps
- Broken URLs
- Model errors
If problems exist, Django displays warnings or errors.
Step 9: Django Loads URL Configuration
Django imports:
ROOT_URLCONFwhich usually points to:
wiki/urls.pyExample:
urlpatterns = [ path("admin/", admin.site.urls), path("", include("encyclopedia.urls")) ]Django now knows how URLs should be routed.
Step 10: Django Creates the Development Server
The
runservercommand creates a lightweight web server.By default:
127.0.0.1:8000This means:
Address: 127.0.0.1 Port: 8000The server begins listening for browser requests.
Step 11: Browser Sends a Request
Suppose the user visits:
http://127.0.0.1:8000/wiki/PythonThe browser sends a request.
Flow:
Browser ↓ Development Server ↓ Django
Step 12: URL Matching Begins
Django examines:
/wiki/Pythonand searches through:
urlpatternsuntil it finds a matching route.
Example:
path("wiki/<str:title>", views.entry)Match found.
Step 13: Django Calls the View
Django executes:
views.entry(request, title)Example:
def entry(request, title): ...The view contains the application’s business logic.
Step 14: The View Generates a Response
The view may:
- Query a database
- Read a Markdown file
- Process a form
- Perform calculations
Eventually it returns:
HttpResponse(...)or
render(...)
Step 15: Django Builds HTML
Example:
return render( request, "encyclopedia/entry.html", context )The template engine combines:
HTML Template + Context Datato produce final HTML.
Step 16: Response Travels Back
The response moves through:
View ↑ Middleware ↑ Development Server ↑ BrowserThe browser receives HTML and displays the page.
Complete Request Lifecycle
python manage.py runserver │ ▼ manage.py │ ▼ Load settings.py │ ▼ Load apps │ ▼ Load middleware │ ▼ Load URLs │ ▼ Start server │ ▼ Browser request │ ▼ URL matching │ ▼ View function │ ▼ Template rendering │ ▼ HTTP response │ ▼ Browser displays page
Why the Development Server Is Special
The server started by:
python manage.py runserveris intended only for development.
Advantages:
- Easy to start
- Automatically reloads when code changes
- Excellent debugging information
Limitations:
- Not optimized for heavy traffic
- Not suitable for production websites
- Less secure than production servers
For production deployments, developers typically use:
- Gunicorn
- uWSGI
- Nginx
- Apache HTTP Server
Key Takeaways
✅
manage.pyis the entry point for Django commands.✅
runservertells Django to start the development server.✅ Django loads settings, apps, middleware, and URL patterns before accepting requests.
✅ Every browser request is routed through URL patterns to a view.
✅ Views generate responses that are returned to the browser.
✅ The development server is perfect for learning and testing but not for production use.
Next Lesson
In the next tutorial we’ll explore:
Understanding Django Project Structure:
manage.py,settings.py,urls.py,wsgi.py, and AppsYou’ll learn what each file does, why Django separates responsibilities into multiple files, and how all the pieces fit together to form a complete web application.
-
AuthorPosts
- You must be logged in to reply to this topic.
