Creating Your First Django Project

What is a Django Project?

A Django project is a directory structure containing settings for an instance of Django, including database and application configurations. It helps developers build dynamic, database-driven websites quickly. The project includes components like settings, URLs, views, and templates to handle both front-end and back-end functions.

Creating a project:

To create a Django project, open the command prompt in the folder where you want to set up your project, then type the following commands to get started.

Creating a Django Project
# Method 1 - Basic Project Creation
django-admin startproject project_name

# Method 2 - Project Inside a Custom Folder
django-admin startproject project_name folder_name

# Example
django-admin startproject django_tutorials

Understanding the Django Project Structure

django_tutorials: The main directory containing the core settings and configuration files.

manage.py: A command-line tool for managing and running Django project tasks.

__init__.py: Marks the directory as a Python package.

settings.py: Contains project configuration like database settings, API keys, and other essentials.

urls.py: Defines the URL routing for the project.

asgi.py: Handles asynchronous server interactions

wsgi.py: Manages server interactions with WSGI-compatible servers like Gunicorn.

To See the Directory Structure, Use the Following Commands in CMD

  • tree: Displays the folder hierarchy of your project.
  • tree /f: Shows the folder structure along with all the files in each folder.
Django Project Structure
django_tutorials/
│
├── manage.py
└── django_tutorials/
    ├── __init__.py
    ├── asgi.py
    ├── urls.py
    ├── settings.py
    └── wsgi.py

Running the Development Server

Start the Django development server using the following commands:

Runserver Commands
# Run the development server on the default port (8000)
python manage.py runserver

# The application will be accessible at:
http://localhost:8000/

# Run the development server on a custom port (e.g., 8080)
python manage.py runserver 8080

# The application will be accessible at:
http://localhost:8080/

# Run server on custom IP and port (e.g., 0.0.0.0:80):
python manage.py runserver 0.0.0.0:80

# The application will be accessible at:
http://localhost/

To access your Django project in a local network, follow these steps:

Access on Local Network
# Step-1: Find your local IP address:
Windows: Type ipconfig in Command Prompt and note the "IPv4" address.
Mac/Linux: Type ifconfig in Terminal and note the "inet" address.

Step-2: Modify settings.py:
ALLOWED_HOSTS = ['*']

Step-3: Run the Django Development Server
python manage.py runserver 0.0.0.0:80

Step-4: Access from Another Device
http://192.168.1.5

# Access from Other Devices on Local Network (e.g., http://192.168.1.5)

Exercise

  1. Fill in the Blanks:
    • - The command `django-admin startproject` is used to __________ a Django project.
    • - To display all files and folders in the project, use the __________ command.
    • - The `settings.py` file contains contains configurations such as __________ and __________.
    • - To allow access from other devices, ALLOWED_HOSTS must include __________.
    • - To find your local IP address on Windows, use the command __________ in CMD.