How to install and use Django

Sanya_Lazy - Sep 19 - - Dev Community

Django

Django is a free and open-source web framework written in Python. It follows the model–template–views architectural pattern and is maintained by the Django Software Foundation. It was first released on July 21, 2005, and is licensed under the 3-clause BSD license. Django is known for its rapid development and clean, pragmatic design, making it a popular choice for building web applications.

Installation:

  • Make sure you have downloaded python. If not install from official python website (link)
  • After installing python complete setup and finish.
  • To check whether the python is correctly installed or not, open terminal and run this
python --version

# output will be python version you have installed
Enter fullscreen mode Exit fullscreen mode

Creating Virtual environment: (optional) but best -

Setting Up Your Virtual Environment:

  • Why Use Virtual Environments? Virtual environments isolate your project's dependencies, preventing conflicts with other projects and ensuring your project has the exact versions of libraries it needs.
  • Creating a Virtual Environment
  • Open your terminal or command prompt.
  • Navigate to the directory where you want to store your project.
  • Use the following command to create a virtual environment (using venv, the standard library option):
# If you want to use with python3 then
python3 -m venv my_env 

# If you want to use with just python then
virtualenv env_name
Enter fullscreen mode Exit fullscreen mode

(Replace my_env with your preferred environment name.)

  • Activating Your Virtual Environment
  • Linux/macOS:
source my_env/bin/activate 
Enter fullscreen mode Exit fullscreen mode
  • Windows:
my_env\Scripts\activate
Enter fullscreen mode Exit fullscreen mode
  • Confirmation: You should see the virtual environment's name in parentheses before your terminal prompt (e.g., (my_env) your_user@your_computer).
  • Creating a Project Folder
  • Inside Your Terminal:
mkdir my_project_name 
Enter fullscreen mode Exit fullscreen mode

(Replace my_project_name with your project's name.)

  • Navigating to the Project Folder
  • Inside Your Terminal:
cd my_project_name
Enter fullscreen mode Exit fullscreen mode
  • Working in Your Project
  • You're Ready to Go! You're now in your project folder with your virtual environment activated. You can start creating your project files, installing dependencies, and running your code.

Installing Django Framework:

  • Install Django package
pip install djangorestframework
Enter fullscreen mode Exit fullscreen mode
  • Create new Django project
django-admin startproject project_name

# change project_name
Enter fullscreen mode Exit fullscreen mode
  • Now after creating project, Navigate to that folder
cd project_name
Enter fullscreen mode Exit fullscreen mode
  • Now create an App in that Django project, by using
django-admin startapp my_app

# you can use any app name in place of my_app
Enter fullscreen mode Exit fullscreen mode
  • Now add my_app in settings.py file
INSTALLED_APPS = [
    ...,
    "my_app",
]
Enter fullscreen mode Exit fullscreen mode
  • If you want to use database then run this (database migrations), in terminal -> in project_folder
python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode
  • Now make migrate
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Testing Django project

  • Start the development server
python manage.py runserver
Enter fullscreen mode Exit fullscreen mode
  • The Django development server starts at http://127.0.0.1:8000.
  • If you want to change port, use this
python manage.py runserver 8001
Enter fullscreen mode Exit fullscreen mode

Happy Coding 😴 - Be Lazy

Contact DM - Twitter(X)
Contact Mail - sanya.san@myyahoo.com

. .
Terabox Video Player