Django App vs Project

Django App vs Project

Everything in Django is centered around the idea of apps. Apps in Django are the self-contained directories that hold files that all work together to provide specific functionality for your Project. A Django project may consist of a single app, or many apps to provide its service. In fact, there is a site dedicated to providing developers all kinds of reusable apps, tools, and more for your Django projects. That site can be found at https://djangopackages.org. In this tutorial, we are going to keep building our Django project and add an app from scratch for a blogging functionality.


Adding An App

We can add a new app to our top-level project folder using the python manage.py startapp command. This app is going to be for managing posts in a blog system, so we are going to just call it posts.

djangoblog $python manage.py startapp posts

After running the command, it should create a new app inside of the Django project like so.

django managepy startapp

Register The App

Anytime you add an app to your Django project, it must be registered in the settings.py file like so.

Files Django Project and Apps

The table here displays all of the files and folders included in the root app created in the Django project vs the files and folders that are created with the startapp command.

Root App of Django Project App created with startapp command
__init__.py migrations
asgi.py __init__.py
settings.py admin.py
urls.py apps.py
views.py models.py
wsgi.py tests.py
views.py

Modularizing URLs

Creating a new app did not create a new urls.py file for us. One convention is to add a urls.py file to any application folders so that you can have routes associated with that app inside its own directory. So we’ll go ahead and add a urls.py file to our posts app now.

django urlspy in apps

Now in the urls.py file, we want to follow the same flow that we have already learned. In our case, when the user visits the app home page, we want to display a list of posts. This will get us started.

posts/urls.py

Now we need to add the post_list() function in the views.py file.

posts/views.py

Namespacing Templates

The function above is trying to render a post_list.html template file that lives in the posts directory. We have not yet created this in the posts app, but we can do that now.

django namespace templates for apps

This is where we come into the namespacing convention when adding a templates folder to an app in Django. By convention, when you add a templates folder to an app, you will then put another folder inside of that which matches the app name. In our case, we have a posts app, so inside of templates, is another folder of posts. That means we can place the post_list.html template file in this folder. Namespacing is needed so that if various apps in the Django project have any template files with the same name, Django will know which template to render.

django template namespace

We can put some simple Html markup to get us started.

posts/templates/posts/post_list.html


Including URLs

In order for urlpatterns that are defined inside of a Django application to have any effect, they need to be included in the main urls.py file of the Django project root. You may recall from a prior tutorial that we already have a few urlpatterns defined in the djangoblog/urls.py file. To activate the urlpatterns in posts/urls.py we can add the following highlighted lines to the base urls.py file.

What the code above does for us is the following. If a user visits the Django app with a browser and includes /posts in the URL, then Django says ok, use the urls.py file that exists in the posts folder for this.

Now we can try visiting http://127.0.0.1:8000/posts/ to see this in action. Our new template file in the posts app is being displayed perfectly!

django template from posts app

Other Resources

Django App vs Project Summary

There are two very common commands you’ll use when working with Django.

  • django-admin.py startproject
  • python manage.py startapp

The first command is to get your Django project started. Once the main project is created, you can use the python manage.py startapp command to add apps to the Django project. A Django project can include many apps, each serving a specific purpose. In this tutorial, we added a posts app that is going to be the application to publish blog posts as we continue with Django. Up next, Models in Django.