DEV Community

Cover image for How to Add a New App to a Django Project: A Step-by-Step Guide
Brian Oginga
Brian Oginga

Posted on

How to Add a New App to a Django Project: A Step-by-Step Guide

In this blog post, we’ll walk through how to create and configure a Django app in an existing Django project. If you’ve been following along with my YouTube video, you’ll know we’re using the Pycouse project and creating a simple app called Home. This guide will show you how to set up a basic function-based view that renders a dynamic title and author to a template.

Let’s get started!

Step 1: Create a New App in Your Django Project
First, navigate to the root of your Django project and create a new app. In this case, we are calling the app "home."

Run the following command in your terminal:

python manage.py startapp home

This command will generate the necessary folder structure for your new app.

Next, add the app to your INSTALLED_APPS list in the settings.py file located in the Pycouse project directory. Open settings.py and add 'home', to the list of installed apps like so:


# pycouse/settings.py

INSTALLED_APPS = [
    ...
    'home',
]

Step 2: Define a Simple View
For this tutorial, we’ll use a function-based view to render a dynamic title and author. In Django, views act as the bridge between your application and the web, and they help you control what content to send to the user's browser.

Open the views.py file inside your home app and define the following view:

`
# home/views.py

from django.shortcuts import render

def index(request):
    context = {
        'title': 'Welcome to Pycouse!',
        'author': 'Brian',
    }
    return render(request, 'home/index.html', context)`

Here, the index view function takes a request object and returns an HTML template called index.html with some dynamic content (title and author) passed via the context dictionary.

Step 3: Create the URL Configuration
Next, we need to map the view to a URL so it can be accessed in the browser. Create a new file called urls.py inside your home app directory, and add the following code:

`
# home/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='home-index'),
]`

This sets up a URL pattern that maps the root URL of the home app (i.e., /) to the index view.

Now, we need to include the home app’s URLs in the main project’s urls.py file. Open the urls.py file located in the Pycouse project directory and modify it as follows:


`# pycouse/urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('home.urls')),  # Include the home app's URLs
]

`
This ensures that when you navigate to the root URL of the project, the index view from the home app is called.

Step 4: Create the Template
Now it’s time to create the HTML template that will display the dynamic content. Inside the home app, create a directory called templates, and inside that, create another directory called home. Your file structure should look like this:


home/
templates/
home/
index.htm

In the index.html file, add the following simple HTML code:

`html
Copy code

<!DOCTYPE html>




{{ title }}


{{ title }}


Author: {{ author }}



`

Here, the template uses Django’s templating language to inject the values for title and author into the HTML.

Step 5: Test It Out!
Now that everything is set up, start the Django development server by running the following command:

`

python manage.py runserver

`
In your browser, navigate to http://127.0.0.1:8000/. You should see the dynamic content rendered on the page:

The title “Welcome to Pycouse!”
The author “Brian”
Conclusion
In this tutorial, we covered how to create and configure a new Django app within an existing project, set up a basic function-based view, map it to a URL, and render dynamic content to an HTML template.

If you want to see this process in action, make sure to check out the full walkthrough on my YouTube video.

Feel free to leave any comments or questions below, and I’ll be happy to help. Happy coding!

Top comments (0)