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.
Register The App
Anytime you add an app to your Django project, it must be registered in the settings.py file like so.
1 2 3 4 5 6 7 8 9 |
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'posts' ] |
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.
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
1 2 3 4 5 6 |
from django.urls import path from . import views urlpatterns = [ path('', views.post_list) ] |
Now we need to add the post_list() function in the views.py file.
posts/views.py
1 2 3 4 5 |
from django.shortcuts import render def post_list(request): return render(request, 'posts/post_list.html') |
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.
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.
We can put some simple Html markup to get us started.
posts/templates/posts/post_list.html
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Posts</title> </head> <body> <h1>Posts List</h1> </body> </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.
1 2 3 4 5 6 7 8 9 10 |
from django.contrib import admin from django.urls import path, include from . import views urlpatterns = [ path('', views.home), path('admin/', admin.site.urls), path('posts/', include('posts.urls')), path('about/', views.about) ] |
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!
Other Resources
- Django apps (realpython.com)
- How to create an analytics dashboard in django app (freecodecamp.org)
- How to build a weather app in django (digitalocean.com)
- Getting Started With Django (hackersandslackers.com)
- Django Skeleton Website (developer.mozilla.org)
- Creating an app with react and django (blog.logrocket.com)
- 33 projects that make developing django apps awesome (gcollazo.com)
- Top 10 django apps and why companies are betting on this framework (netguru.com)
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.