What’s the difference between a project and an app in Django world?

Question:

I am creating my first real website using Django, but I am still struggling to understand the difference between a project and an app. For example, my website is a sports news website which will contain sections like articles, ranking tables and "fixtures and results".

My questions are:

  • Should each one of these sections be in a separate app inside a whole project or not?
  • What is the best practice in this situation?
Asked By: Alex

||

Answers:

Ideally, your project should be composed by apps. That’s why when using the command line, you create a project, an later on, add apps to that project.

Apps, aims to bring modularity to your project. For example, if you build an articles app, ideally, you can use it in your sports news project, and re-use it in a new project which requires it with minimum or no modification to its settings — say a blog project, for example.

Apps are piece of software meant to be reused. Your project stands only for your very specific needs.

Take a look at Django Project Structure. It may give you some insight in the best practice of organizing your Django project.

There are also several blog posts searchable on Google that address this topic:

Answered By: Paulo Bu

A project refers to the entire application and all its parts.

An app refers to a submodule of the project. It’s self-sufficient and not intertwined with the other apps in the project such that, in theory, you could pick it up and plop it down into another project without any modification. An app typically has its own models.py (which might actually be empty). You might think of it as a standalone python module. A simple project might only have one app.

For your example, the project is the whole website. You might structure it so there is an app for articles, an app for ranking tables, and an app for fixtures and results. If they need to interact with each other, they do it through well-documented public classes and accessor methods.

The main thing to keep in mind is this level of interdependence between the apps. In practice it’s all one project, so there’s no sense in going overboard, but keep in mind how co-dependent two apps are. If you find one app is solving two problems, split them into two apps. If you find two apps are so intertwined you could never reuse one without the other, combine them into a single app.

Answered By: John Mee

Lets understand Project and App in Django with this realistic example:

Say you are building an online shopping site (e-commerce site) in Django:

Project:

Its simply name of your website. Django will create a python package and give it a name that you have provided. lets say we name it my_shopping_site.

You can create a project in Django with this command

python manage.py startproject my_shopping_site

This will create my_shopping_site directory in your working directory and the structure will look like this:

my_shopping_site/
   manage.py
   my_shopping_site/    # package
        __init__.py     # indication of package
        settings.py     # module 1
        urls.py         # module 2
        wsgi.py         # module 3

Apps:

Its those little components that together make up your project. They are the features of your project. In our case (shopping site) it would be:

  • Cart :- Which would have a logic for user selected items for purchase.

  • Products :- Which would have a logic for products that the site is selling.

  • Profile:- Which would have a logic for user information.

    -----------------------------------------------------------
    my_shopping_site              Products     Profile     Cart
    -----------------------------------------------------------
    

and you can create these apps with these commands:

python manage.py startapp cart
python manage.py startapp products
python manage.py startapp profile

The structure would look like this:

my_shopping_site/   # project name
      manage.py
      products/          # app 1
      cart/              # app 2 
      profile/           # app 3
      my_shopping_site/

each app focus on a single logical piece of your project.

Answered By: Ahtisham

It says in the Writing your first Django app, part 1 article of the official Django documentation:

Projects vs. apps

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database ofpublic records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain
multiple apps. An app can be in multiple projects.

The project example in the Securely Deploy a Django App With Gunicorn, Nginx, & HTTPS article drives this home better than the one in the documentation though (quoting with some changes in the figure):

$ cd  # Change directory to home directory
$ python3 -m venv .venv
$ source .venv/bin/activate

# NOW INSTALL DJANGO 3.2
$ python -m pip install -U pip 'django==3.2.*'

$ mkdir django-gunicorn-nginx/
$ django-admin startproject project django-gunicorn-nginx/

$ cd django-gunicorn-nginx/
$ django-admin startapp myapp

$ python manage.py migrate
$ mkdir -pv myapp/templates/myapp/

This creates the Django app myapp alongside the project named project:

/home/ubuntu/
│                             
├── django-gunicorn-nginx/    | CONTAINER
│    │                        /
│    │
│    ├── myapp/               
│    │   ├── admin.py         |
│    │   ├── apps.py          |
│    │   ├── __init__.py      |
│    │   ├── migrations/      | APP 1.
│    │   ├── models.py        |
│    │   ├── templates/       |
│    │   ├── tests.py         |
│    │   └── views.py         /
│    │
│    ├── another_app/         
│    │   ├── __init__.py      | APP 2.
│    │   └── ...              /
.    .
│    ├── one_of_many_app/     
│    │   ├── __init__.py      | APP n.
│    │   └── ...              /
│    │
│    ├── project/             
│    │   ├── asgi.py          |
│    │   ├── __init__.py      |
│    │   ├── settings.py      | PROJECT  
│    │   ├── urls.py          | CONFIGS
│    │   └── wsgi.py          |   &
|    |                        | CONTROLS
│    ├── db.sqlite3           |
│    └── manage.py            /
│
└── .venv/  ← Virtual environment
Answered By: Krishnadas PC
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.