Building Your First Django Application: A Step-by-Step Guide

In the previous blog post, we introduced Django and discussed its key features and advantages. In this blog post, we will walk you through setting up a Django development environment and building your first Django application.

Prerequisites

Before you begin, you will need to install Python and Django on your computer. Follow these steps to install Python and Django:

1. Install Python: Download and install Python from the official python website.

2. Install Django: Open a terminal or command prompt and type the following command:

pip install django

This will install the latest version of Django.

3. Verify Django installation: To verify that Django is installed correctly, type the following command:

django-admin --version

This should display the version of Django that you just installed.

Creating a Django Project

Once you have installed Python and Django, you can create your first Django project. Follow these steps:

1. Open a terminal or command prompt and navigate to the directory where you want to create your project.

2. Type the following command to create a new Django project:

django-admin startproject myproject

This will create a new directory called myproject with the following structure:

myproject/

├── manage.py

└── myproject/

├── __init__.py

├── settings.py

├── urls.py

└── wsgi.py

The manage.py file is a command-line utility that you can use to interact with your Django project.

3. Start the development server: Navigate to the myproject directory and type the following command to start the development server:

python manage.py runserver

This will start the development server at http://localhost:8000.

Creating a Django App

In Django, a project consists of one or more apps. An app is a self-contained module that provides a specific functionality. Follow these steps to create a new app in your Django project:

1. Navigate to the myproject directory and type the following command to create a new app called myapp:

python manage.py startapp myapp

This will create a new directory called myapp with the following structure:

myapp/

├── __init__.py

├── admin.py

├── apps.py

├── models.py

├── tests.py

└── views.py

The ``views.py`` file contains the logic for handling requests and generating responses.

2. Create a view: Open the views.py file and add the following code:

from django.http import HttpResponse

def hello(request):

return HttpResponse("Hello, world!")

This creates a simple view that returns a "Hello, world!" message.

3. Create a URL pattern: Open the urls.py file in the myapp directory and add the following code:

from django.urls import path

from . import views

urlpatterns = [

path('hello/', views.hello, name='hello'),

]

This maps the URL path /hello/ to the hello view.

4. Include the URL pattern in the project: Open the urls.py file in the myproject directory and add the following code:

from django.urls import include, path

urlpatterns = [

path('myapp/', include('myapp.urls')),

]

This includes the URL pattern for the myapp app in the project.

5. Test the app: Restart the development server by typing the following command:

python manage.py

This will start the development server. Open up your web browser and go to localhost:8000. You should see the text "Hello, world!" displayed on the page.

🎉 Congratulations! You have just built your first Django app. In the next lesson, we'll continue building our app and learn more about Django's features.