131π
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.
55π
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.
- [Django]-Django middleware difference between process_request and process_view
- [Django]-Django composite unique on multiple model fields
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
19π
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:
- [Django]-Retrieving a Foreign Key value with django-rest-framework serializers
- [Django]-Django β "Incorrect type. Expected pk value, received str" error
- [Django]-Login Page by using django forms
3π
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
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-Django error when installing Graphite β settings.DATABASES is improperly configured. Please supply the ENGINE value
- [Django]-Django south migration β Adding FULLTEXT indexes