Django

Django Apps

Create Django apps

Django Apps

In Django, an "app" is a Python package that contains a set of related functionality. A project can contain multiple apps, and an app can be used in multiple projects.

Create a Django App

To create a new app, use the startapp command:

python manage.py startapp myapp

This creates a directory called myapp with the following structure:

myapp/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
    migrations/
        __init__.py

App Structure

  • models.py: Define your database models here
  • views.py: Define your views (request handlers) here
  • admin.py: Register your models for the admin interface
  • apps.py: App configuration
  • tests.py: Write tests for your app
  • migrations/: Database migration files

Register App in Settings

Add your app to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',  # Add your app here
]

App vs Project

A project is a collection of configuration and apps for a particular website. An app is a web application that does something specific. A project can contain multiple apps, and an app can be in multiple projects.