Your First Django Project

Introduction

With Django installed in your virtual environment, this chapter creates a project and app, registers the app, wires a Hello Django view to the site root, and runs the development server on http://127.0.0.1:8000/. You will use manage.py for the first time—the CLI entry point for almost all Django work.

Prerequisites

Project Layout After This Chapter

We use config as the project package name (common convention). The app is blog.

Step 1: Create the Project

From hello-django/ (only .venv and git files so far):

bash
django-admin startproject config .

The trailing . puts manage.py in the current folder instead of a nested config/config/.

Verify:

bash
ls          # macOS/Linux
dir         # Windows
python manage.py --version

Code explanation:

  • django-admin — global CLI shipped with Django (uses your venv’s Django)
  • startproject config — creates Python package config with settings.py, urls.py, etc.
  • manage.py — project-specific wrapper; prefer it over django-admin for daily tasks

Step 2: Create an Application

bash
python manage.py startapp blog

Code explanation:

  • A project holds site-wide config; an app is a feature module (blog, users, shop)
  • blog/ gets models.py, views.py, admin.py, empty migrations/

Step 3: Register the App

Edit config/settings.py. Find INSTALLED_APPS and add 'blog':

python
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "blog",  # your app
]

Code explanation:

  • Django only loads apps listed here (models, admin, templates, static discovery)
  • Typo in app name → models/migrations silently missing—double-check spelling

Step 4: Write a View

Edit blog/views.py:

python
from django.http import HttpResponse
 
 
def home(request):
    return HttpResponse("Hello Django")

Code explanation:

  • requestHttpRequest with method, headers, GET/POST data
  • HttpResponse — plain text or HTML body returned to the browser
  • Every view accepts request as the first argument

Step 5: Wire URLs

App-level URLs

Create blog/urls.py:

python
from django.urls import path
 
from . import views
 
urlpatterns = [
    path("", views.home, name="home"),
]

Project-level URLs

Edit config/urls.py:

python
from django.contrib import admin
from django.urls import include, path
 
urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("blog.urls")),
]

Code explanation:

  • include("blog.urls") mounts the app’s URLconf at the site root
  • name="home" enables {% url 'home' %} and reverse("home") later
  • admin/ is built-in; you will use it after createsuperuser in a later chapter

Step 6: Run the Development Server

bash
python manage.py runserver

Default: http://127.0.0.1:8000/

Custom port:

bash
python manage.py runserver 8080

Verify:

bash
curl http://127.0.0.1:8000/

Expected body: Hello Django

Browser: open http://127.0.0.1:8000/ or http://localhost:8000/

Stop the server: Ctrl+C in the terminal.

Warning

Development Server Only

runserver is for local learning. Never expose it to the public internet as production—use Gunicorn + Nginx in deploy chapters.

Step 7: System Check

Before relying on new config, run:

bash
python manage.py check

Expected: System check identified no issues (0 silenced).

Catches common settings.py and URL mistakes early.

manage.py Commands You Will Use Often

CommandPurpose
runserverStart dev server
checkValidate project configuration
startapp nameCreate a new app
migrateApply database migrations (next chapters)
makemigrationsCreate migration files from models
createsuperuserAdmin login user
shellDjango-aware Python REPL
testRun tests

Help:

bash
python manage.py help
python manage.py help migrate

Default Site and Admin (Quick Look)

With server running:

URLExpected
http://127.0.0.1:8000/Hello Django
http://127.0.0.1:8000/admin/Admin login (no user yet—setup in Admin chapter)

Common Problems

SymptomFix
CommandError: You must set settings.DJANGO_SETTINGS_MODULERun commands from folder containing manage.py
ModuleNotFoundError: No module named 'blog'Add 'blog' to INSTALLED_APPS
404 on /Check config/urls.py includes blog.urls; view wired to ""
python manage.py uses wrong DjangoActivate .venv; check which python
Port 8000 in userunserver 8001 or stop the other process
Page shows Django welcome rocket (old tutorial)You may have a different root view—ensure blog.urls maps "" to home

Post-Chapter Checklist

  • django-admin startproject config . created manage.py and config/
  • blog app created and listed in INSTALLED_APPS
  • blog/views.py returns Hello Django
  • blog/urls.py + config/urls.py include pattern works
  • python manage.py runserver + browser/curl shows greeting
  • python manage.py check passes

FAQ

django-admin vs manage.py?

manage.py sets DJANGO_SETTINGS_MODULE for this project. Prefer python manage.py for daily work.

Why project name config?

Avoids clashing with common app names like blog or shop. Any valid Python package name works (mysite, core).

Can I skip creating an app?

Django expects features in apps. Minimal demos can use one app; real projects use several.

Hot reload?

runserver reloads Python code on save. Some settings changes need a manual restart.

SQLite created yet?

Not until you define models and run migrate. No database file required for this Hello view.

Same as Flask app.run()?

Similar developer experience; Django adds manage.py, INSTALLED_APPS, and split project/app layout.