Running and Managing the Django Development Server

Tutorial 4 of 5

Django Development Server Tutorial

1. Introduction

In this tutorial, we aim to explain how to run and manage the Django development server. The Django development server is a lightweight server provided by Django for development purposes. You'll learn how to start, stop, and manage this server, which is crucial to the development and testing of your Django applications.

By the end of this tutorial, you will be able to:

  • Run the Django development server
  • Stop the server when necessary
  • Understand how to handle common server-related tasks

Prerequisites: Basic understanding of Django and Python, and Django installed on your machine.

2. Step-by-Step Guide

Django is a high-level Python Web framework that allows rapid development and clean, pragmatic design. It comes with a lightweight server for developing and testing applications. Here's how you can manage this server:

Running the Server

To start the server, navigate to your Django project directory in your terminal and run the following command:

python manage.py runserver

This command starts the server at http://127.0.0.1:8000/ (localhost on port 8000). You can specify a different IP address or port by adding them as arguments to the runserver command.

Stopping the Server

To stop the server, you can press CTRL + C in your terminal where the server is running.

3. Code Examples

Example 1: Running the server on a specific port

python manage.py runserver 7000

This command will start the server at http://127.0.0.1:7000/. This is useful when the default port (8000) is being used by another service.

Example 2: Running the server on a different IP

python manage.py runserver 192.168.0.2:8000

This will start the server at http://192.168.0.2:8000/. This is useful when you want to make your development server accessible from other devices on your network.

Note: Don't use the Django development server in a production setting. It is intended only for development purposes.

4. Summary

In this tutorial, we've covered how to start and stop the Django development server. We've also shown how to run the server on a different IP address and port. These are fundamental skills for developing Django applications.

Next, you might want to learn more about Django's various features and components, such as the Django ORM, Django Templates, and Django Forms.

Additional resources:
- Django Official Documentation
- Django for Beginners
- Django Tutorial on Mozilla Developer Network

5. Practice Exercises

  1. Exercise 1: Start the Django development server on port 9000.

Solution:
bash python manage.py runserver 9000
This command will start the server at http://127.0.0.1:9000/.

  1. Exercise 2: Stop the server running on your machine.

Solution:
Press CTRL + C in your terminal where the server is running. This command will stop the server.

  1. Exercise 3: Start the Django development server on a different IP address (use any local address different from 127.0.0.1).

Solution:
bash python manage.py runserver 192.168.0.2:8000
This command will start the server at http://192.168.0.2:8000/.

Remember, practice is key to mastering any skill. Keep experimenting with different commands and options.