Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 2. Django Quick Start Guide

Set Up Virtual Environment

Set Up Virtual Environment

Setting Up a Virtual Environment for Django

When you develop or run an application, the application will utilize a set of other software programs which are interlinked with each other (called “dependencies”).

For example, your computer runs on a particular OS (e.g., Mac OS X Yosemite). And on the OS, particular versions of the programing language (e.g., Python 3.8), web framework (e.g., Django 3.2), and/or many libraries are installed.

When you develop a Django application, you need to pay attention to the dependencies. Your application can adequately run on the dependencies you configured; however, it may not work correctly on another computer as versions of libraries, python, or Django itself may differ across computers. The environment dependent on each computer is called the computing environment.

What Is Virtual Environment?

A virtual Environment is a concept or tool used to create a specific environment for an application by segregating it from other computer resources. There are several ways to create a virtual environment. Using virtual machine software is one choice. The recent popular approach is using a Docker container. But these approaches take time to set up. Python has a feature to create a virtual environment by running a simple command.

venv

venv is a command to create a virtual environment in your project directory with a few steps.

Step 1: Create a virtual environment directory

In the project directory, run the venv command with a virtual environment name you want to create. The example below uses d_env as the virtual environment name.

Command Line - INPUT
python3 -m venv d_env

Note: For Windows, use python instead of python3.

When you run the command, the d_env directory is created directly under the project. The dependency information will be saved under this directory throughout your code development.

Set Up Virtual Environment

Step 2: Activate the virtual environment

To create a virtual environment in your project directory, you need to activate your virtual environment by running the source command. For Mac, the activate file is stored under the bin directory under the virtual environment directory you created in the previous step.

Run the command below to activate the virtual environment named d_env.

Command Line - INPUT
source d_env/bin/activate

When you run the command, there will be a change in the command line interface like the one below. (d_env) means the virtual environment named d_env is now active.

Command Line - INPUT
 

When you install Django, you need to make sure that your virtual environment is active to manage your new app's dependencies properly.

For Windows (Powershell)windows.svg

For Windows, the command used to activate the virtual environment is different from the one on Mac.

When activating the virtual environment for the first time, you must first run the command below.

Command Line - INPUT
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Then, run the command below to activate the virtual environment.

Command Line - INPUT
.\d_env\Scripts\Activate.ps1

Step 3: Deactivate

You can run the deactivate command when you want to return to the normal mode.

Command Line - INPUT
deactivate

After running the command, you'll see that the command-line interface goes back to normal.

Command Line - INPUT
 

IdeaNote: venv directory location

The venv directory location should not be changed. If you move the directory, python will refer to the original absolute path, and you won't be able to run the Python command properly.

Thus, you must also be careful when moving your project directory. If you move your project directory, your venv directory will also move.

If you want to change the location of the project directory, delete the existing venv directory and create a new venv directory under a new path. If you are using the same requirements.txt, you should be able to recover the virtual environment fully.

Also, you need to make sure that you don't edit files and directories under the venv directory during your coding.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Creating and Editing Django Models

Create Django Models

Email Verification with Django Allauth: Console Method

Django Allauth (3) – Email Verification via Console

Creating and Editing Django Models

Create Django Models

Email Verification with Django Allauth: Console Method

Django Allauth (3) – Email Verification via Console

Tags:

venv

Virtual Environment

Dependencies

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment