Apps and Routes
Goal for this part
Replace the default rocket with Campus Press routes you control. You will create a blog app, add home and about views, and include the app’s URLs from the project root.
After this snapshot, visiting / and /about/ should show plain text (or simple responses) from your own view functions—proof that Django’s URL dispatcher is calling your code.
What you should already know
You should be able to run manage.py runserver from the Getting Started snapshot. Comfort with Python functions is enough; you do not need prior experience with Django apps.
Think of an app as a feature package: models, views, templates, and URLs that belong together. The project settings file decides which apps are active.
Concepts: projects, apps, and URL includes
You create an app with python manage.py startapp blog, then list 'blog' in INSTALLED_APPS. Until it is installed, Django will not load that app’s models or ready hooks.
Root urls.py can include() an app’s URL module so blog paths stay in blog/urls.py. That keeps the project root clean as you add users, profiles, and more routes later.
# campus_press/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("blog.urls")),
]
Walkthrough: views and app URLs
A view function receives an HttpRequest and returns an HttpResponse. For now, returning a short string is enough to prove routing works before templates appear in the next part.
In blog/urls.py, map "" to home and "about/" to about. Name your URL patterns early—name="blog-home"—so templates can reverse them later without hard-coding paths.
# blog/views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Campus Press — home")
def about(request):
return HttpResponse("About Campus Press")
# blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.home, name="blog-home"),
path("about/", views.about, name="blog-about"),
]
How to run it and what you should see
Run the 02-Apps-And-Routes snapshot with the usual venv and install steps. Open http://127.0.0.1:8000/ and http://127.0.0.1:8000/about/. You should see your view text, not the default rocket page.
git clone https://github.com/michaeldunga1/fcc-django-blog.git
cd fcc-django-blog/02-Apps-And-Routes
python3 -m venv .venv
source .venv/bin/activate
pip install -r ../requirements.txt
python manage.py runserver
Common mistakes and troubleshooting
A 404 on about often means the path is missing a trailing slash expectation or the app URLs were never included from the project root. An ImproperlyConfigured error can mean the app is missing from INSTALLED_APPS or the include string is wrong.
If both URLs still show the rocket, you may be running the wrong snapshot folder or an old server process. Stop with Ctrl+C and start again from 02-Apps-And-Routes.
Try this
Add a third named route such as /contact/ that returns a short sentence. Then change only the view text and refresh—confirm you know which file Django executes for each path.
- Project = settings + root URLs; app = feature package
- Use
include()to keep blog routes in the blog app - Name URL patterns from the start
Next: Templates
Comments
One comment per signed-in account. Comments are saved with this page’s URL.