DEV Community

Cover image for Beginner's Guide to Setting Up a Django Project
Rupesh Mishra
Rupesh Mishra

Posted on

Beginner's Guide to Setting Up a Django Project

Django is a powerful web framework for Python that allows you to build web applications quickly and efficiently. This guide will walk you through the process of setting up a Django project from scratch, perfect for beginners who want to get started with web development using Django.

Table of Contents

  1. Installing Python
  2. Setting Up a Virtual Environment
  3. Installing Django
  4. Creating a Django Project
  5. Creating a Django App
  6. Configuring the Database
  7. Creating Models
  8. Creating Views
  9. Creating Templates
  10. Configuring URLs
  11. Running the Development Server

1. Installing Python

Before we start with Django, make sure you have Python installed on your system. Different Django version requires different Python versions.

To check if Python is installed, open your terminal or command prompt and type:

python --version
Enter fullscreen mode Exit fullscreen mode

If Python is not installed, download and install it from the official Python website (https://www.python.org/downloads/).

2. Setting Up a Virtual Environment

It's a good practice to create a virtual environment for each Django project. This keeps your project dependencies isolated from other projects.

To create a virtual environment:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command:
python -m venv myenv
Enter fullscreen mode Exit fullscreen mode

This creates a new virtual environment named "myenv".

To activate the virtual environment:

  • On Windows:
myenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode
  • On macOS and Linux:
source myenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

You should see your prompt change to indicate that the virtual environment is active.

3. Installing Django

With your virtual environment activated, install Django using pip:

pip install django
Enter fullscreen mode Exit fullscreen mode

4. Creating a Django Project

Now that Django is installed, let's create a new project:

django-admin startproject myproject
Enter fullscreen mode Exit fullscreen mode

This creates a new directory called "myproject" with the basic Django project structure.

Navigate into the project directory:

cd myproject
Enter fullscreen mode Exit fullscreen mode

5. Creating a Django App

Django projects are made up of one or more apps. Let's create an app for our project:

python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

This creates a new directory called "myapp" with the basic app structure.

Open the myproject/settings.py file and add your new 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',
    'myapp',  # Add this line
]
Enter fullscreen mode Exit fullscreen mode

6. Configuring the Database

Django uses SQLite as its default database, which is fine for development. The database configuration is already set up in myproject/settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
Enter fullscreen mode Exit fullscreen mode

To create the database tables, run:

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

7. Creating Models

Models define the structure of your database. Let's create a simple model in myapp/models.py:

from django.db import models

class Item(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __str__(self):
        return self.name
Enter fullscreen mode Exit fullscreen mode

After creating or modifying models, run these commands to create and apply migrations:

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

8. Creating Views

Views handle the logic of your application. Create a simple view in myapp/views.py:

from django.shortcuts import render
from .models import Item

def item_list(request):
    items = Item.objects.all()
    return render(request, 'myapp/item_list.html', {'items': items})
Enter fullscreen mode Exit fullscreen mode

9. Creating Templates

Templates are HTML files that define how your data is displayed. Create a new directory myapp/templates/myapp/ and add a file item_list.html:

<!DOCTYPE html>
<html>
<head>
    <title>Item List</title>
</head>
<body>
    <h1>Items</h1>
    <ul>
    {% for item in items %}
        <li>{{ item.name }} - {{ item.description }}</li>
    {% endfor %}
    </ul>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

10. Configuring URLs

To make your view accessible, you need to configure URLs. First, in myproject/urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]
Enter fullscreen mode Exit fullscreen mode

Then, create a new file myapp/urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.item_list, name='item_list'),
]
Enter fullscreen mode Exit fullscreen mode

11. Running the Development Server

You're now ready to run your Django project! Use this command:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Visit http://127.0.0.1:8000/ in your web browser to see your Django application in action.

Congratulations! You've set up a basic Django project. This foundation will allow you to expand your project by adding more models, views, and templates as needed.

Follow me on my social media platforms for more updates and insights:

Remember to always activate your virtual environment before working on your project, and to run migrations whenever you make changes to your models.

Top comments (0)