DEV Community

Shan Ndanu
Shan Ndanu

Posted on

DAY 4: How I Structured My Django Project with Two Apps

hello again. I actually find it interesting learning new things in django day by day.so proud of myself. Recently, I embarked on a new Django project which i named it Shannels, and i structured it with two distinct apps . I wanted to share my experience, the thought process behind it, and the tools I used to achieve this modularity.

i actually found out that idea behind Django's "app" concept is to promote the "single responsibility principle." Each app should do one thing and do it well.

The Setup: Creating the Project and Apps

First, I created the main Django project, Shannels. I like to name my project's root directory the same as the project itself, to keep things clean.

django-admin startproject shannels
cd shannels

Next, I created my two apps within the shannels directory.

python manage.py startapp users
python manage.py startapp content

At this point, my project directory structure looked something like this:

shannels/
├── manage.py
├── shannels/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py
├── users/
│ ├── migrations/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── content/
├── migrations/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py

Wiring It All Together: settings.py and urls.py
i saw that the best and easier way to make Django aware of your new apps is to register them in settings.py. and that what i tried.

In shannels/settings.py, I added my apps to the INSTALLED_APPS list:

shannels/settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My apps
'users',
'content',
]

Next, I needed to define the URL routing. Each app has its own urls.py file to manage its specific routes. Then, the main project's urls.py includes these app-specific URLs. for sure i had trouble at first but i tried.

First, createurls.py files within each app's directory

`from django.urls import path

from . import views

urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about')
]
`

Almost there. In the main project's shannels/urls.py, I included these app-specific URL configurations:
`shannels/urls.py

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

urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('users.urls')), # URLs for user management
path('blog/', include('content.urls')), # URLs for content management
]

Now, URLs like
/accounts/register/ would be handled by the users app, and /blog/articles/ by the content` app

Shared Resources
While designing the app i saw it was common for them to share resources . Here's how I managed that:

Templates: I set up a project-level templates directory in shannels/templates for base templates (e.g., base.html, navbar.html) that are shared across all apps. Each app could then have its own templates subdirectory (e.g., users/templates/users/, content/templates/content/) for app-specific templates that often extend the base templates. This is configured in settings.py by adding BASE_DIR / 'templates' to TEMPLATES['DIRS'].

Model Relationships: If a model in one app needs to relate to a model in another i saw that Django handles this easily. i simply imported my model

content/models.py

from django.db import models
from users.models import CustomUser # Assuming you have a CustomUser model

class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
author = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
published_date = models.DateTimeField(auto_now_add=True)

What I Used Beyond Django Itself
While the core functionality of structuring multiple apps is built into Django, here are some tools and practices that made the process smoother:

(Python venv): Absolutely essential for isolating project dependencies. I always start by creating a virtual environment: python -m venv venv and activating it.
Git for Version Control: Indispensable for tracking changes, experimenting with features, and collaborating.
sensible Naming Conventions
Django Admin: Once the models are defined in each app, registering them withadmin.py in their respective apps immediately gives you a powerful interface for managing your data

What i learnt

  • it is not always hard when you try and learn from mistakes

-if you anticipate growth or distinct functionalities, consider starting with multiple app

  • don't split your project into apps that are too granular.

**To finish up **i can tell that Creating a Django project with multiple apps isn't just about doing it to best practices; it's about building and maintainable. By dividing my project's concerns into users and content apps within Shannels, I've laid a strong foundation for future development and collaboration. I recommend this approach for my next Django endeavor!

Top comments (0)