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

PythonDjango

Python Problem Overview


I am creating my first real website using Django but I am still struggling to understand the diff 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 question is 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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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:

Solution 4 - Python

As per 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 of > public 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - PythonJohn MeeView Answer on Stackoverflow
Solution 2 - PythonAhtishamView Answer on Stackoverflow
Solution 3 - PythonPaulo BuView Answer on Stackoverflow
Solution 4 - PythonKrishnadas PCView Answer on Stackoverflow