If you're just getting started with Django, this guide will walk you
step-by-step through creating your first project and app. By the end,
you'll understand how to set up Django, create models, and run your
project in your browser. Let's dive in!
π Step 1: Project Setup
Before creating a Django project, we need to set up our environment. A
great tool for managing Python dependencies is Pipenv.
Open your terminal and run:
pip install pipenv # install pipenv (one-time setup)
pipenv shell # activate virtual environment
pipenv install django # install Django
This ensures that your project has its own isolated environment, keeping
things clean and organized.
π Step 2: Create a Django Project
Once Django is installed, create a new project:
django-admin startproject project_name .
Here, replace project_name
with the name of your choice (e.g.,
myproject
). The .
at the end makes sure the project is created in
the current directory.
π¦ Step 3: Create a Django App
In Django, apps are modular components that handle specific
functionality (like blog, accounts, contact, etc.).
Run the following command to create an app:
python manage.py startapp app_name
Replace app_name
with your preferred name (e.g., contacts
).
βοΈ Step 4: Configure Your Project
After creating the app, we need to tell Django about it. Open
settings.py
inside your project folder and add your app to the
INSTALLED_APPS
list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name', # add your app here
]
π€ Step 5: Database Setup & Superuser
Before we can use Django's built-in admin panel, let's set up the
database.
Run:
python manage.py migrate
This applies the default database migrations.
Now, create a superuser to log in to the admin panel:
python manage.py createsuperuser
You'll be asked to enter: - Username - Email - Password
βΆοΈ Step 6: Run Your Development Server
Start the server:
python manage.py runserver
Visit your site in the browser:
- Project home: http://127.0.0.1:8000/\
- Admin panel: http://127.0.0.1:8000/admin
Login using the superuser credentials you created.
π Step 7: Create Your First Model
Models define the structure of your database tables. Let's create a
simple Contact model.
Open your app's models.py
and add:
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
phone = models.CharField(max_length=15)
address = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
π Step 8: Apply Model Migrations
Whenever you add or modify models, you need to run migrations:
python manage.py makemigrations
python manage.py migrate
This creates the necessary database tables.
π You're All Set!
Now you have a working Django project with: - A project setup\
- An app added\
- A database configured\
- A model created
You can expand this project by creating views, templates, and URLs to
display your data.
β
Next Steps for You - Learn about Django views and
templates\
- Build CRUD (Create, Read, Update, Delete) functionality\
- Explore Django REST Framework for APIs
π This tutorial should help any beginner start their Django journey
smoothly.
Top comments (0)