How to Create a Django Project: Step-by-Step Guide
Django is a high-level Python web framework that enables rapid development and clean, pragmatic design. This guide will walk you through setting up your first Django project, ensuring best practices and a smooth start to your web development journey.
Prerequisites
Python: Ensure Python 3.8 or newer is installed. Check with:
python --version
pip: Confirm pip is available:
pip --version
Virtual Environment (Recommended): Using a virtual environment isolates your project’s dependencies and avoids conflicts.
Create and activate a virtual environment:
python -m venv venv # On Linux/macOS: source venv/bin/activate # On Windows: venv\Scripts\activate
Step 1: Install Django
With your virtual environment activated, install Django using pip:
pip install django
This command fetches the latest stable version of Django and its dependencies.
Step 2: Start a New Django Project
Navigate to the directory where you want your project to reside, then run:
django-admin startproject myproject
Replace myproject
with your preferred project name. This creates a new directory containing the initial Django project structure:
manage.py
[project_name]/__init__.py
[project_name]/settings.py
[project_name]/urls.py
[project_name]/asgi.py
[project_name]/wsgi.py
Step 3: Run the Development Server
Change into your project directory:
cd myproject
Start the development server:
python manage.py runserver
If successful, you’ll see output indicating the server is running at http://127.0.0.1:8000/
. Open this URL in your browser to view the default Django welcome page.
Next Steps
You now have a working Django project! Here are some suggestions for what to do next:
Create an app: Django projects are composed of one or more apps. Start with:
python manage.py startapp myapp
Explore settings: Review and adjust
settings.py
for your needs (e.g., database configuration, installed apps).Version control: Initialize a Git repository to track your project’s changes:
git init echo "venv/" >> .gitignore git add . git commit -m "Initial Django project setup"
Tips
Always activate your virtual environment before working on your project.
Use descriptive names for your projects and apps.
Refer to the official Django documentation for in-depth guidance.
Congratulations! You’ve set up your first Django project and are ready to start building robust web applications.