Django

Install Django and Create Your First Project (Step-by-Step Guide)

πŸ“Œ Introduction

Django is a powerful Python web framework used to build scalable and secure web applications.

In this first program, you will learn how to:

  • Install Django
  • Create a virtual environment
  • Create your first Django project
  • Run the development server

🎯 Program Statement

πŸ‘‰ Install pip, create and activate a virtual environment, install Django, create a project, and run the development server.


βš™οΈ Step 1: Check Python Installation

Open Command Prompt and type:

python --version

If Python is installed, it will show version like:

Python 3.x.x

βš™οΈ Step 2: Install pip (if not installed)

python -m ensurepip --upgrade

Check pip:

pip --version

βš™οΈ Step 3: Create Virtual Environment

Navigate to your project folder:

mkdir django_project
cd django_project

Create virtual environment:

python -m venv env

βš™οΈ Step 4: Activate Virtual Environment

πŸ‘‰ Windows:

env\Scripts\activate

πŸ‘‰ Mac/Linux:

source env/bin/activate

After activation, you will see:

(env) C:\...

βš™οΈ Step 5: Install Django

pip install django

Check installation:

django-admin --version

βš™οΈ Step 6: Create Django Project

django-admin startproject myproject

Go inside project:

cd myproject

πŸ“‚ Project Structure

After creating project, you will see:

myproject/
β”‚
β”œβ”€β”€ manage.py
└── myproject/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ settings.py
β”œβ”€β”€ urls.py
β”œβ”€β”€ asgi.py
└── wsgi.py

βš™οΈ Step 7: Run Development Server

python manage.py runserver

Output:

Starting development server at http://127.0.0.1:8000/

🌐 Step 8: Open Browser

Go to:

πŸ‘‰ http://127.0.0.1:8000/

You will see:

βœ” Django default welcome page


🧠 Explanation

  • Virtual Environment β†’ Keeps project dependencies isolated
  • Django β†’ Framework to build web apps
  • manage.py β†’ Command-line tool for Django
  • runserver β†’ Starts local server

⚠️ Common Errors & Fixes

❌ Error: ‘python not recognized’

πŸ‘‰ Install Python and add to PATH


❌ Error: ‘django-admin not found’

πŸ‘‰ Use:

python -m django --version

❌ Error: Port already in use

python manage.py runserver 8001

πŸ§ͺ Practice Questions

  1. Install Django in another folder
  2. Create project named collegeproject
  3. Run server on port 9000
  4. Check Django version

🎀 Viva Questions

1. What is Django?

Django is a high-level Python web framework used to build secure and scalable web applications quickly. It follows the MVT (Model-View-Template) architecture.


2. What is a virtual environment?

A virtual environment is an isolated space where Python dependencies are installed separately for each project. It prevents conflicts between different project libraries.


3. Why do we use pip?

pip is a package manager for Python used to install and manage external libraries like Django. It helps in maintaining project dependencies easily.


4. What is django-admin?

django-admin is a command-line utility used to perform administrative tasks like creating a project. It helps in setting up the initial Django structure.


5. What is the use of manage.py?

manage.py is a command-line tool specific to a Django project. It allows running the server, migrations, and other project-related commands.


6. What does runserver do?

The runserver command starts Django’s development server locally. It allows you to test your application in a browser.


7. What is the default port of Django server?

By default, Django runs on port 8000. You can change it by specifying a different port in the command.


8. What is a Django project?

A Django project is the main container that holds configurations and multiple apps. It manages the overall structure of the application.


9. What is the role of settings.py?

settings.py contains configuration settings such as installed apps, database settings, and static files. It controls how the project behaves.


10. What is the use of urls.py?

urls.py maps URLs to views. It defines which function should run when a user visits a particular URL.


πŸ‘‰ Next Post: Create Django App and Register It
πŸ‘‰ Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *