In this lesson, you will learn how to run the Django development server, open your Django project in a browser, understand the server address, change the port, and stop the server.
Django includes a lightweight development server that allows you to run your project locally while building and testing it.
You do not need to configure a separate web server just to start learning and developing a Django application.
The development server makes it easy to test your Django application on your own computer.
Before starting the server, make sure Django is installed in your current Python environment.
python -m django --version
If the command displays a version number, Django is available.
Open the terminal and move into the directory that contains
manage.py.
For example:
cd MyDjangoProject
Django management commands are commonly executed from this directory.
A basic Django project contains a manage.py file.
MyDjangoProject/
manage.py
myproject/
settings.py
urls.py
asgi.py
wsgi.py
Run the development server from the directory containing this file.
The basic command for starting Django's development server is:
python manage.py runserver
Django starts the local development server.
After starting the server, Django normally displays a local address similar to:
http://127.0.0.1:8000/
Open this address in your web browser to view the project.
127.0.0.1 is a loopback address that refers to your own
computer.
When you open the Django development server at this address, the request is handled locally on your computer.
A port identifies a communication endpoint used by applications.
Django commonly starts its development server on port 8000.
http://127.0.0.1:8000/
After starting the server, open your preferred web browser.
Enter:
http://127.0.0.1:8000/
Django will display the default development page if the project is configured correctly.
A newly created Django project can display Django's default welcome page when the development server is running successfully.
This confirms that the basic project can be started and accessed locally.
During development, Django can automatically reload the development server when Python code changes are detected.
This makes development faster because you generally do not need to manually restart the server after every code change.
You can start the development server on a different port by providing the port number.
python manage.py runserver 8080
The server will then be available at a local address using port
8080.
You can choose another available port, for example:
python manage.py runserver 5000
Then open:
http://127.0.0.1:5000/
Django's development server can also be started with a specific address and port.
python manage.py runserver 127.0.0.1:8080
This starts the development server on the specified local address and port.
For certain development and testing situations, the server can be bound to an address that allows connections from other devices on the network.
python manage.py runserver 0.0.0.0:8000
Network configuration and firewall settings may affect whether another device can connect.
Django's development server is convenient for development and testing, but it is not designed to be a production deployment server.
Production deployments normally use a suitable production web server and application server configuration.
When the server starts, the terminal displays information about the development server.
It can also display requests received by the application while you browse the site.
When you visit the local Django URL, your browser sends an HTTP request to the development server.
Django processes the request and returns an HTTP response.
Later, you will create URL patterns that connect URLs to views.
For example, a URL can be connected to a Python function that generates the response for that page.
urlpatterns = [
...
]
To stop the development server, return to the terminal where it is running and press:
Ctrl + C
The server will stop and the terminal will return to the command prompt.
After stopping the server, you can start it again whenever you need it.
python manage.py runserver
Django will start the local server again.
If the server cannot start because the port is already being used, choose another port.
python manage.py runserver 8080
This can allow you to continue development using another available port.
If Django reports that manage.py cannot be found, you may
be in the wrong directory.
Move to the directory containing manage.py and run the command
again.
If Django is installed inside a virtual environment, make sure the environment is activated before running the project.
Windows:
venv\Scripts\activate
Linux/macOS:
source venv/bin/activate
cd MyDjangoProject
venv\Scripts\activate
python manage.py runserver
Then open the local address shown in the terminal.
cd MyDjangoProject
source venv/bin/activate
python manage.py runserver
Then open the local server address in your browser.
A simple Django development cycle is:
The most commonly used command is:
python manage.py runserver
The default local address is commonly:
http://127.0.0.1:8000/
You can specify another port when needed.
Django's development server provides a convenient way to run and test your application locally.
python manage.py runserver
Open the local address in your browser, develop and test your application,
and press Ctrl + C to stop the server.
You are now ready to learn how Django's project configuration works.
python manage.py runserver to start the development server.127.0.0.1 refers to the local computer.python manage.py runserver 8080.manage.py.Ctrl + C to stop the development server.Question: Which command starts the Django development server?