Step-by-Step Setup Guide
This guide ensures you follow best practices by setting up a dedicated virtual environment.
Prerequisites
Before starting, ensure you have Python 3.8 or newer installed on your device. You can verify this by running:
python3 --version
Phase 1: Environment Setup
We start by creating an isolated environment for your project, which is standard practice for professional development.
Step 1: Create Project Directory
>> Open your terminal or command prompt, navigate to your preferred development folder, and create the project's root directory. Put the folder name as "todo_list"
Step 2: Open the todo_list folder and open it in the terminal
>> Go to the todo_list directory and open a terminal in that directory.
Step 3: Create a Virtual Environment (venv)
Create a dedicated virtual environment named venv inside your new project folder.
python3 -m venv venv
Step 4: Activate the Virtual Environment
Activation links your current terminal session to this environment, meaning any packages installed will only affect this project. You must do this every time you open a new terminal for this project.
|
Operating System |
Command to Run |
|---|---|
|
macOS / Linux |
|
|
Windows (CMD) |
|
|
Windows (PowerShell) |
|
Verification: Once activated, your terminal prompt should change, typically starting with (venv).
Phase 2: Installation and Dependencies
Now, we install Django and record the exact version being used.
Step 5: Install Django
Install the Django framework into your active virtual environment.
# Install the latest stable version of Django
(venv) $ pip install django
Phase 3: Project and App Creation
Step 6: Start the Django Project
Start the main configuration project, which we will call todo_list. The use of . is a best practice to keep the file structure clean.
# Start the project configuration files in the current directory
(venv) $ django-admin startproject <project name> .
Adjust the project name as per your project name. For this tutorial, we will keep it as "todo_list"
The above command will start the project in the current directory. Optionally, you can directly run "django-admin startproject <project name>" without the dot. This will start a new project in the current directory by creating an entirely new directory.
Step 7: Test the Server
Run the development server to confirm the setup is complete.
(venv) $ python manage.py runserver
Navigate to http://127.0.0.1:8000/ in your browser. If you see the welcome page, you are ready to code!
Step 8: Create the To-Do App
Create the specific application, tasks, which will hold all the logic (models, views, templates) for your to-do list features.
# Create the tasks application
(venv) $ python manage.py startapp <app name>
Adjust the app name as per your app name. For this tutorial, we will keep it as "tasks"
Step 9: Configure settings.py (Register the App)
Open the main project settings file at todo_list/settings.py and register your new tasks app by adding its name to the INSTALLED_APPS list.
In todo_list/settings.py:
INSTALLED_APPS = [
# ... (other default apps)
'django.contrib.staticfiles',
# --- ADD YOUR NEW APP HERE ---
'tasks',
]
Step 10: Run Initial Migrations
The migrate command sets up the default database tables needed for Django's built-in features (like the Admin panel and User Authentication).
# Create database migrations for changes (if any)
(venv) $ python manage.py makemigrations
# Apply the migrations to create the tables (including default Django tables)
(venv) $ python manage.py migrate
Step 11: Test the Server
Run the development server to confirm the setup is complete.
(venv) $ python manage.py runserver
Navigate to http://127.0.0.1:8000/ in your browser. If you see the welcome page, you are ready to code!
This structure is now ready for you to define your Task model in tasks/models.py.
Navigate To Next Tutorial (CRUD):
https://lalitmahato.com.np/blog/django-crud-operations-step-by-step-guide/