🎓 A1Learner
🔍
0 of 50 lessons completed

Creating a Django App

startapp is the command that makes a new app. It writes the files of an empty app for you.

Run startapp

This terminal line makes an app named blog. Run it in the project folder.

python manage.py startapp blog

The files it makes

The command writes seven files. The list below is the real result of the command.

find blog -type f | sort

The app config

apps.py names the app config BlogConfig. The name line says the app is blog.

from django.apps import AppConfig


class BlogConfig(AppConfig):
    name = 'blog'

The empty views file

views.py starts with one import and a comment. The views go below the comment.

from django.shortcuts import render

# Create your views here.

Example

import os
import tempfile

import django
from django.conf import settings

settings.configure(DEBUG=True, SECRET_KEY="key", ROOT_URLCONF=__name__, ALLOWED_HOSTS=["*"])
django.setup()

from django.core.management import call_command

os.chdir(tempfile.mkdtemp())
call_command("startapp", "blog")
found = []
for root, dirs, files in os.walk("blog"):
    for name in files:
        found.append(os.path.join(root, name))
for path in sorted(found):
    print(path)

Key points

  • python manage.py startapp blog makes an app named blog.
  • The command writes seven files, with views and models.
  • apps.py holds the app config, named BlogConfig.
  • Add the app to INSTALLED_APPS after making it.

In this example, the setup lines configure Django. The program works in a fresh temporary folder, so the files land in one clean place. call_command runs a management command from inside a program. Here it runs startapp for an app named blog. The walk gathers every file under blog. The prints show the seven files in order.

Note

startapp writes files, so run it once per app.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does python manage.py startapp blog do?