DEV Community

Cover image for Day Four of My Django Bootcamp: Crafting the Structure of My Django Project
@rinnah
@rinnah

Posted on

Day Four of My Django Bootcamp: Crafting the Structure of My Django Project

Day Four of My Django Bootcamp: Crafting the Structure of My Django Project

Today is the fourth day of my Django bootcamp, and it has been an exciting journey so far! I focused on creating and structuring my Django project while learning a lot about apps, templates, and URL configurations. Here’s a friendly walkthrough of how I accomplished it using Git Bash as my terminal.


1. Starting the Django Project 🚀

The first step was to create a new Django project named dijango. This project would serve as the foundation for everything else. Using Git Bash, I navigated to my desired directory and set up a virtual environment:

mkdir dijango
cd dijango
python -m venv venv
source venv/bin/activate  # For Linux/macOS
venv\Scripts\activate   # For Windows
Enter fullscreen mode Exit fullscreen mode

Next, I installed Django and created the project:

pip install django
django-admin startproject dijango .
Enter fullscreen mode Exit fullscreen mode

Here’s what the structure looked like at this point:

  • manage.py: The project’s control center.
  • dijango/: A directory containing core files like settings.py, urls.py, and others.

2. Creating Applications 🛠️

Django encourages splitting functionality into smaller units called apps. I created two apps, REE1 and REE2, to separate different functionalities:

python manage.py startapp REE1
python manage.py startapp REE2
Enter fullscreen mode Exit fullscreen mode

Each app came with its own files, like views.py and models.py. To make Django recognize these apps, I added them to the INSTALLED_APPS section in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'REE1',
    'REE2',
]
Enter fullscreen mode Exit fullscreen mode

3. Setting Up Templates 🎨

Templates define how the front-end of the app looks. Using Git Bash, I created a templates directory in the root folder and added subfolders for each app:

mkdir templates
mkdir templates/REE1
mkdir templates/REE2
Enter fullscreen mode Exit fullscreen mode

In settings.py, I updated the TEMPLATES configuration to include the new directory:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Enter fullscreen mode Exit fullscreen mode

4. Configuring URLs 🌐

URL configurations connect specific views to URLs. Since Django doesn’t create urls.py files for apps by default, I manually added them for REE1 and REE2.

For REE1/urls.py:

from django.urls import path
from . import views

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

For REE2/urls.py:
jy

from django.urls import path
from . import views

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

I then updated the main project’s urls.py to include these app-specific routes:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('ree1/', include('REE1.urls')),
    path('ree2/', include('REE2.urls')),
]
Enter fullscreen mode Exit fullscreen mode

5. Adding Views and Templates 🖼️

In Django, views determine what gets displayed for each URL. I created simple views for both apps:

For REE1/views.py:

from django.shortcuts import render

def index(request):
    return render(request, 'REE1/index.html')
Enter fullscreen mode Exit fullscreen mode

For REE2/views.py:

from django.shortcuts import render

def home(request):
    return render(request, 'REE2/home.html')
Enter fullscreen mode Exit fullscreen mode

Next, I added basic HTML templates:

templates/REE1/index.html:

<!DOCTYPE html>
<html>
<head>
    <title>REE1 Index</title>
</head>
<body>
    <h1>Hello I am Rinnah. Welcome to REE1 app index page.</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

templates/REE2/home.html:

<!DOCTYPE html>
<html>
<head>
    <title>REE2 Home</title>
</head>
<body>
    <h1>Welcome to REE2!</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This is the outcome of after testing

Image description


Using Git Bash throughout this process made it easy to execute commands and navigate between directories. As I continue exploring Django, I look forward to building more complex projects and honing my skills. If you’re on a similar journey, let’s connect and share our progress!

Top comments (0)