π 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:
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
- Install Django in another folder
- Create project named
collegeproject - Run server on port 9000
- 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
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
