Django Development Server 🎯

beginner
23 min

Django Development Server 🎯

Welcome to our in-depth Django Development Server tutorial! By the end of this lesson, you'll have a solid understanding of how to set up and use the Django development server for your projects. πŸ“ Note: Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.

Getting Started πŸ“

Before we dive in, make sure you have Python and Django installed on your system. You can find the installation guide in our Python Tutorial and Django Tutorial - Installation.

Creating a New Project πŸ“

Let's start by creating a new Django project:

bash
django-admin startproject myproject cd myproject

πŸ’‘ Pro Tip: Replace myproject with the name you'd like to give your project.

Running the Development Server πŸ’‘

To start the Django development server, navigate to your project directory and run:

bash
python manage.py runserver

This command starts the development server and opens a URL on your web browser. By default, it's http://127.0.0.1:8000/. πŸ“ Note: The server runs on port 8000, and you can change this later in the settings.py file.

Code Examples πŸ’»

Let's create a simple app within our project to better understand the development server.

bash
python manage.py startapp myapp

Now, edit the myapp/views.py file to include the following code:

python
from django.http import HttpResponse def hello(request): return HttpResponse("Hello, World!")

Next, open the myapp/urls.py file and add the following content:

python
from django.urls import path from . import views urlpatterns = [ path('', views.hello, name='hello'), ]

Now, go to your project's urls.py file and make sure to include the app's URL configuration:

python
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]

Finally, refresh your web browser and navigate to http://127.0.0.1:8000/myapp/ to see the "Hello, World!" message. πŸŽ‰

Quick Quiz
Question 1 of 1

What command starts the Django development server?

Common Development Server Commands πŸ“

Here are some common development server commands you'll use during your projects:

  • runserver: Starts the development server
  • runmigrate: Creates and applies initial database migrations
  • createsuperuser: Creates a superuser for the Django admin interface
  • makemigrations: Creates migration files for your app's models
  • migrate: Applies the migration files to the database

Wrapping Up βœ…

Congratulations! You've learned how to set up and use the Django development server. In our next lesson, we'll explore Django's admin interface and learn how to create custom user models. Keep up the great work! πŸš€