How to install and use Django
Sanya_Lazy
Posted on September 19, 2024
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 django
- 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
insettings.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 - sanya.san@myyahoo.com
💖 💪 🙅 🚩
Sanya_Lazy
Posted on September 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.