liveBook · Manning

Startproject command

As explained in Django in Action

The startproject command is a Django administrative command that creates the initial files and directories for a Django project.

The Django `startproject` command creates the starting structure for a website: a project directory, a `manage.py` control script, and a configuration package. It is run through `django-admin`, Django’s entry point for configuration activities. Django is a framework, so this generated structure gives Django the files it needs to locate settings, URLs, and server entry points before application code is added. section 2.2 section 2.9

Create a project in the current directory

After activating an environment with Django installed, run: `django-admin startproject RiffMates .` `RiffMates` is the project name; the final `.` places the generated files in the current directory. Without that location argument, `startproject` creates a new directory for the project. The resulting layout includes `manage.py` beside a same-named configuration directory—a naming arrangement that can make “the RiffMates directory” delightfully ambiguous. section 2.2

RiffMates/
├── RiffMates
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

Inspect the generated tree to distinguish the outer project folder from the inner configuration package and see where `manage.py`, `settings.py`, `urls.py`, `asgi.py`, and `wsgi.py` live. section 2.2

`manage.py` is a project-local counterpart to `django-admin`: use it for control and configuration actions after creation. For example, `python manage.py runserver` starts the development server. The configuration package contains `settings.py` for configuration and `urls.py` for URL mappings; `wsgi.py` is part of the web-server path into Django, and `asgi.py` is also generated. section 2.2 section 2.9

Keep the generated project structure separate from the code that implements site features:

  • A Django project names the overall website and holds its configuration package. section 2.9
  • A Django app is a module-oriented unit for site code; create one with `python manage.py startapp` rather than putting every view in the project’s `urls.py`. section 2.9 section 2.3
  • Register each app in `INSTALLED_APPS`, then map URLs to views that receive an `HttpRequest` and return an `HttpResponse`. section 2.9
  • Name apps for what they do, especially as a project grows; apps can be self-contained and may also be installed from third parties. section 2.3

Project names can follow ordinary Python module conventions, such as lowercase snake case, though the generated directory structure uses the name supplied to the command. Before choosing where to create it, decide whether a virtual environment belongs alongside the project or in a shared location; Django projects are directories, so either arrangement can fit the workflow. section 2.2

How Django in Action covers it

How deep it goes
Explains the command’s generated directories and key files, then defers feature code to separately created apps.
Examples
Demonstrates `django-admin startproject RiffMates .` and a generated tree containing manage.py and configuration files.
What it assumes
Assumes a Python environment with Django installed while explaining Django project and app terminology from zero.
What you can do after
Operate/configure it by creating a Django project and using its manage.py script for project commands.
How it connects
Connects Django projects with django-admin, manage.py, settings.py, urls.py, apps, views, and WSGI.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.