Creating a project and apps with Django

Building web applications with Python & Django

🕑 This lesson will take about 20 minutes

What is Django?

In Django, there is a parent-child relationship. At the top level is a project (which can be, for example, a website) and the project can contain apps (where each app can be a different section of a website or web application). You build a project that contains one or more apps. The project encompasses the entire website/web application. A very large website may contain several apps. Often, you may only need to build one project with one app if it is a small website.

Create a new project

To create a new project, use the following command (where project is the name of your project) making sure you include the period (.) at the end:

django-admin startproject project .

Note: The trailing period ( . ) at the end of the command (after the space) is important. It instructs django-admin to use the current directory you’re working in for your project rather than creating a sub-directory. If you leave off the period at the end of the command, it will create an additional sub-directory.

If this command doesn’t work, try one of the following commands:

python -m django startproject project

or

python3 -m django startproject project

or

django-admin.py startproject project

If you still experience any issues, you may want to try using the bash terminal instead of PowerShell (if using Windows) or zsh if using Mac.

Screenshot of startproject command

You’ll notice there are a couple of new files including:

  • manage.py is the entry point of the application and contains code that is used to perform a lot of the operations needed to manage the application such as updating the database or creating super users, etc.

  • urls.py is another file created which is used to set up the routing for the application (eg. the server takes a request which will be sent to this file which will then direct to a function or method that needs to be called or another routing table that will ultimately send a response to the user).

  • settings.py is a file that contains settings for the applications (eg. turning debugging on/off, the apps which are part of the project, etc).

Create the first app

Run the following command to create your first app (where myapp is the name of your app):

django-admin startapp myapp 

If this doesn’t work, you can also try this command:

py -m django startapp 'myapp'

 After running this command, you’ll notice a new directory which is the name of your app (eg. myapp). It will contain some files, eg:

  • admin.py - used to register modules and configure the built-in admin site

  • models.py - used to set up database models

  • tests.py - used to create tests

  • views.py - used to create view functions/classes

  • apps.py - contains the configuration for the app (make a note of the name of the config from here eg. MyappConfig as you’ll need to reference it in the next step)

Register the first app

We now need to make sure we register myapp as an installed app in the project. Go to settings.py in the project directory (within the main directory) and scroll down to the line of code containing the list called INSTALLED_APPS.

Add the following line to the INSTALLED_APPS list in the settings.py file (making sure to refer to the name of your app and the configuration name):

'myapp.apps.MyappConfig',

Screenshot of the INSTALLED_APPS list in the settings.py file

Your first project and app are now set up and ready for coding! In the next lesson, we will run the server for the first time and then write some code for the app.

Next lesson: Hello, world!