How to Create a Django Project: A Step-by-Step Tutorial
Django is a powerful and popular Python web framework that simplifies the process of building web applications rapidly. This tutorial will guide you through creating your first Django project—from setting up the environment to running your new web app.
Prerequisites
- Basic knowledge of Python programming.
- Python 3 installed on your computer. You can download it from the official Python website.
- Command-line interface familiarity (Terminal on macOS/Linux or Command Prompt/PowerShell on Windows).
Step 1: Install Django
Before creating a project, you need Django installed in your environment. It’s best practice to use a virtual environment to manage your project dependencies.
# Create a virtual environment (optional but recommended)
python3 -m venv myenv
# Activate the virtual environment
# On macOS/Linux:
source myenv/bin/activate
# On Windows:
myenv\Scripts\activate
# Install Django using pip
pip install django
Step 2: Create a New Django Project
Once Django is installed, you can create a new project using the django-admin command.
django-admin startproject myproject
This creates a new directory named myproject containing the project files.
Project Structure Overview
manage.py: A command-line utility to interact with the project.myproject/: The actual Python package for your project.__init__.py: An empty file that tells Python this directory should be considered a Python package.settings.py: Configuration for your project.urls.py: URL declarations for your project.wsgi.py: Entry-point for WSGI-compatible web servers to serve your project.
Step 3: Run the Development Server
Navigate into your project directory and run the Django development server to verify your project works.
cd myproject
python manage.py runserver
Open your browser and go to http://127.0.0.1:8000/. You should see the Django welcome page stating \u201cThe install worked successfully!\u201d
Step 4: Create Your First App
Django projects are composed of apps. To create one:
python manage.py startapp myapp
This command creates a new app folder myapp with several files for views, models, and more.
Register the App
Open myproject/settings.py and add 'myapp' to the INSTALLED_APPS list:
INSTALLED_APPS = [
#... existing apps,
'myapp',
]
Step 5: Create a Simple View and URL
Modify myapp/views.py to add a simple view:
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello, Django! Your project is set up successfully.')
Next, create a URL pattern for the view. Create a new file myapp/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Then, include the app URLs in the project’s main urls.py (myproject/urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
Step 6: Test the App
Run the server again:
python manage.py runserver
Visit http://127.0.0.1:8000/ in your browser. You should see Hello, Django! Your project is set up successfully.
Troubleshooting Common Issues
- Command not found errors: Ensure that Python and Django are properly installed and your virtual environment is activated.
- Port already in use: By default, Django server uses port 8000. You can specify another port like this:
python manage.py runserver 8080 - App not found or 404 errors: Make sure to register your app in
settings.pyand that URLs are properly included.
Summary Checklist
- Install Python and set up a virtual environment.
- Install Django using pip.
- Create a new Django project with
django-admin startproject. - Run the development server and check for the welcome page.
- Create a new app with
manage.py startapp. - Register the app in
settings.py. - Define views and URLs for your app.
- Run the server and verify your app displays the expected content.
For additional learning on Python frameworks and tools, check out our post on How to Install Python Django Framework: Step-by-Step Guide for detailed installation instructions and tips.
With this tutorial, you are now ready to explore Django’s powerful features and build your own advanced web applications!
