"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to install and use Django

How to install and use Django

Published on 2024-11-09
Browse:305

How to install and use Django

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

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

(Replace my_env with your preferred environment name.)

  • Activating Your Virtual Environment
  • Linux/macOS:
source my_env/bin/activate 
  • Windows:
my_env\Scripts\activate
  • 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 

(Replace my_project_name with your project's name.)

  • Navigating to the Project Folder
  • Inside Your Terminal:
cd my_project_name
  • 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
  • Create new Django project
django-admin startproject project_name

# change project_name
  • Now after creating project, Navigate to that folder
cd project_name
  • 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
  • Now add my_app in settings.py file
INSTALLED_APPS = [
    ...,
    "my_app",
]
  • If you want to use database then run this (database migrations), in terminal -> in project_folder
python manage.py makemigrations
  • Now make migrate
python manage.py migrate

Testing Django project

  • Start the development server
python manage.py runserver
  • 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

Happy Coding ? - Be Lazy

Contact DM - Twitter(X)
Contact Mail - [email protected]

Release Statement This article is reproduced at: https://dev.to/sanya_lazy/how-to-install-and-use-django-3928?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3