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.
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.
Let's start by creating a new Django project:
django-admin startproject myproject
cd myprojectπ‘ Pro Tip: Replace myproject with the name you'd like to give your project.
To start the Django development server, navigate to your project directory and run:
python manage.py runserverThis 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.
Let's create a simple app within our project to better understand the development server.
python manage.py startapp myappNow, edit the myapp/views.py file to include the following code:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, World!")Next, open the myapp/urls.py file and add the following content:
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:
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. π
What command starts the Django development server?
Here are some common development server commands you'll use during your projects:
runserver: Starts the development serverrunmigrate: Creates and applies initial database migrationscreatesuperuser: Creates a superuser for the Django admin interfacemakemigrations: Creates migration files for your app's modelsmigrate: Applies the migration files to the databaseCongratulations! 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! π