Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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?

like image 220
Alex Avatar asked Oct 13 '13 22:10

Alex


People also ask

What is a Django app?

A Django application is a Python package that is specifically intended for use in a Django project. An application may use common Django conventions, such as having models , tests , urls , and views submodules.

How many apps can a Django project have?

Django comes with six built-in apps that we can examine.

Why do I need an app in Django?

Benefits of using Django apps –Django apps are reusable i.e. a Django app can be used with multiple projects. Debugging and code organization is easy. Django has an excellent debugger tool.


2 Answers

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.

like image 70
John Mee Avatar answered Sep 28 '22 03:09

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.

like image 26
Ahtisham Avatar answered Sep 28 '22 02:09

Ahtisham