Lorenzo Bianchi Logo

Getting Started with Django

Date Published

Getting Started with Django

Django is a powerful Python framework for building secure and scalable web applications.
This guide will walk you through setting up your development environment and creating your first Django project from scratch.


✅ Prerequisites

Before you begin, make sure you have:

  • Python 3.8 or higher installed
  • pip, the Python package manager
  • A terminal or command prompt
  • Basic understanding of Python (functions, classes, etc.)

You can verify your Python version with:

1python --version

⚙️ Step 1: Set Up a Virtual Environment

A virtual environment keeps your project’s dependencies isolated from other Python projects.

1.Create a virtual environment:

1python -m venv myenv

2.Activate the environment
On Windows:

1myenv\Scripts\activate

On macOS / Linux:

1source myenv/bin/activate

💡 When activated, you’ll see (myenv) at the beginning of your terminal line.


📦 Step 2: Install Django

Once your environment is active, install Django using pip:

1pip install django

Check your Django version:

1django-admin --version

💡 Tip: Always keep a requirements.txt file to record dependencies:

pip freeze > requirements.txt

🏗️ Step 3: Create a New Django Project

Now create your first Django project:

1django-admin startproject myproject
2cd myproject

This command generates the basic Django project structure, including configuration files and the built-in development server.


🧩 Step 4: Create a New App

Django projects are made up of smaller applications.
Let’s create one called myapp:

1python manage.py startapp myapp

Next, register your new app in myproject/settings.py:

1INSTALLED_APPS = [
2 'django.contrib.admin',
3 'django.contrib.auth',
4 'django.contrib.contenttypes',
5 'django.contrib.sessions',
6 'django.contrib.messages',
7 'django.contrib.staticfiles',
8 'myapp', # 👈 Add this line
9]

🧱 Step 5: Initial Setup

1. Create database tables

1python manage.py migrate

This applies Django’s default migrations and sets up the database tables.

2. Create a superuser (admin)

1python manage.py createsuperuser

Follow the prompts to create an admin user with a username, email, and password.


🚀 Step 6: Run the Development Server

Start the built-in Django server:

1python manage.py runserver

Then open your browser and visit:

👉 http://127.0.0.1:8000/

You’ll see Django’s welcome page!
To access the admin interface, go to:
👉 http://127.0.0.1:8000/admin/


🗂️ Project Structure Overview

After these steps, your directory should look like this:

1myproject/
2├── manage.py
3├── myproject/
4│ ├── __init__.py
5│ ├── asgi.py
6│ ├── settings.py
7│ ├── urls.py
8│ └── wsgi.py
9└── myapp/
10 ├── __init__.py
11 ├── admin.py
12 ├── apps.py
13 ├── models.py
14 ├── tests.py
15 └── views.py

🧭 Step 7: Add a Simple View and URL

Let’s display a simple message in your browser.
Open myapp/views.py and add:

1from django.http import HttpResponse
2
3def home(request):
4 return HttpResponse("Hello, Django!")
5

Then create myapp/urls.py (if it doesn’t exist):

1from django.urls import path
2from . import views
3
4urlpatterns = [
5 path('', views.home, name='home'),
6]
7

Finally, connect your app’s URLs to the main project in myproject/urls.py:

1from django.contrib import admin
2from django.urls import path, include
3
4urlpatterns = [
5 path('admin/', admin.site.urls),
6 path('', include('myapp.urls')), # 👈 Add this line
7]
8

Now, reload http://127.0.0.1:8000/
and you should see "Hello, Django!" 🎉


🧩 Next Steps

From here, you can start expanding your app:

  • 🗃️ Define models in models.py
  • 🧠 Build views in views.py
  • 🌐 Set up routes in urls.py
  • 🖼️ Create HTML templates in a templates/ folder
  • 🎨 Add static files (CSS, JS, images) in a static/ folder

💡 Best Practices

1. Always activate your virtual environment before working:

1source myenv/bin/activate

2. Keep your dependency list up to date:

1pip freeze > requirements.txt
2

3. Add a .gitignore file to exclude temporary files and virtual environments.
4. Use Git (or another version control system) to track your project changes.


📚 Learn More

If you want to explore Django in depth, the official documentation is the best place to continue learning. It covers everything from models and templates to deployment and security best practices:

👉 Official Django Documentation: https://docs.djangoproject.com/


Other Useful Resources: