In this section, we will guide you through the process of creating your first Django project. By the end of this lesson, you will have a basic Django project up and running.

Prerequisites

Before you start, ensure you have:

  • Python installed on your machine.
  • Django installed (if not, refer to the previous section on setting up the development environment).

Steps to Create Your First Django Project

  1. Create a Project Directory

First, create a directory where your Django project will reside. Open your terminal or command prompt and run:

mkdir my_django_project
cd my_django_project

  1. Create a Virtual Environment

It's a good practice to create a virtual environment for your project to manage dependencies. Run the following commands:

python -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`

  1. Install Django

With the virtual environment activated, install Django using pip:

pip install django

  1. Create a Django Project

Use the django-admin command to create a new Django project. Replace myproject with your desired project name:

django-admin startproject myproject

This command creates a new directory named myproject with the following structure:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py

  1. Understand the Project Structure

Here's a brief overview of the files and directories created:

  • manage.py: A command-line utility that lets you interact with your Django project.
  • myproject/: The inner directory containing the project settings and configurations.
    • __init__.py: An empty file that tells Python this directory should be considered a package.
    • settings.py: Configuration settings for the project.
    • urls.py: URL declarations for the project.
    • wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

  1. Run the Development Server

Navigate to the outer myproject directory and run the development server:

cd myproject
python manage.py runserver

You should see output similar to this:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open your web browser and go to http://127.0.0.1:8000/. You should see the Django welcome page, indicating that your project is set up correctly.

  1. Create a Django App

Django projects are composed of multiple apps. To create an app, run the following command inside your project directory:

python manage.py startapp myapp

This creates a new directory named myapp with the following structure:

myapp/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
    migrations/
        __init__.py

  1. Register the App

To make Django aware of your new app, you need to add it to the INSTALLED_APPS list in settings.py:

# myproject/settings.py

INSTALLED_APPS = [
    ...
    'myapp',
]

  1. Create a Simple View

Let's create a simple view in myapp/views.py:

# myapp/views.py

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, world! This is my first Django app.")

  1. Map the View to a URL

Create a urls.py file in the myapp directory and map the view to a URL:

# myapp/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

Include this URL configuration in the project's urls.py:

# myproject/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

  1. Test the View

Run the development server again:

python manage.py runserver

Navigate to http://127.0.0.1:8000/ in your web browser. You should see the message "Hello, world! This is my first Django app."

Summary

In this section, you learned how to:

  • Set up a Django project.
  • Understand the basic project structure.
  • Create and register a Django app.
  • Create a simple view and map it to a URL.

You now have a basic Django project up and running. In the next section, we will dive deeper into Django's project structure and explore its components in more detail.

© Copyright 2024. All rights reserved