Tutorial goal: Access shell, virtual environment, Create a new app, install it
In this tutorial, we will add another app to our project.
Start a new Django app
Before starting this section, let’s talk a little about Django apps and the difference between an app and the project. Consider a big company with multiple departments. The big company is analogous to a Django project and each department is like an app. Each department has it’s own facilities and functionalities. Each department is concerned with a specific set of problems but all of these departments work for a common goal. Likewise, a Django project contains multiple apps that each one of which acts on a specific part of the problem. For example, we may create an app called “account” to deal with signups, logins, forgot passwords, and any account-related problem. Or we may create a payment app to deal with payments, coupons invoices, and anything payment-related. Having multiple apps instead of one giant app has various advantages. First, this makes it easy for you to organize your code. Second, your code will be reusable and you can use a different app for a different website. If you make a payment app in one project, you may be able to reusable a big chunk of it for another website.
Creating a new app in Django is super easy. First, you need to activate your virtual environment, and then, you need to specify the title of the new app. Naming an app something meaningful is very important because it will make it easier to communicate the purpose of the app to other developers in your team and also yourself in the future.
python manage.py startapp song
As we mentioned in the previous tutorials, manage.py is a file in the root of your Django project used for executing many Django-specific tasks like creating a new app within a project, running the development server, running your tests, and more. After executing the command above, a folder with the name as your app (song) is created inside the project file. So far all of our project hierarchy of folders looks something like this:
structure of folders of our project so far.
By creating (starting) a new Django app, it will not be picked up by Django automatically, unless we tell Django that we have a new app and it should be installed. This kind of configuration (settings) is done in the settings.py file in the main app folder. So open your settings.py file and let’s install our newly created app. Find the section of installed apps which is a list not-surprisingly named INSTALLED_APPS! It should look like this by default
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
Django by default has some pre-installed app that is provided for developers’ convenience. If you open settings.py in the project directory you can see this list of preinstalled apps. You can install a Django app by providing its name to the list of INSTALLED_APPS in settings.py Now we add the name of our new app to this list.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'song', ]
Thank you for reading, I’ll see you in the next tutorial. 🙂
If you wish to join a community of self-starters learning new skills and creating projects from scratch, join our Discord channel here and share your passion.
Django Tutorial for beginners [step by step] part 4