How to Create a Django App and URL Routes

TL;DR
Create a Django blog app with python manage.py startapp blog, define a view that returns an HttpResponse, and map that view through app-level and project-level URL patterns. The project route uses include to forward /blog requests to the blog app, where an empty path calls the home view and displays the blog home page.
Transcript
hey there how's it going everybody in this video we'll be adding a blog application to our Django site and also setting up some URL routes so that we can start directing people to pages that we'd like them to see so let's go ahead and get started so first let's create a blog app for our site now a lot of people might get confused at this part and t... Read More
Key Insights
- A Django project is a website container that can hold multiple applications, such as separate blog and store sections. This organization keeps different parts of a project separated while allowing an individual app to be added to multiple website projects.
- The blog application is created by running
python manage.py startapp blogin the same directory asmanage.py. The command generates a new blog directory with its own structure while leaving the previously created project directory andmanage.pyunchanged. - A Django view is a function that handles traffic for a route and returns what the visitor should see. The
homeview accepts a request argument and returns anHttpResponsecontaining the wordsBlog Homewrapped in anh1HTML element. - An app-level URL configuration is created in a new
blog/urls.pyfile. It importspathfromdjango.urls, imports the localviewsmodule, and defines aurlpatternslist that connects URL paths to view functions. - The blog homepage route uses an empty string as its app-level path because the project-level configuration already matches the
/blogprefix. The empty path maps the remaining URL toviews.home, causing the blog home response to be returned. - The route name
blog-homeis more specific than a generic name such ashome. A specific name reduces the possibility of collisions during reverse lookups when another application, such as a store, also defines its own home route. - The project-level URL configuration uses
includefromdjango.urlsto forward requests beginning with/blogtoblog.urls. Django then evaluates the patterns in that app-level file to determine which view should handle the remaining path. - A 404 response occurs when the requested address matches none of the configured URL patterns. After the routes are added, the website root does not show the earlier default development page because Django attempts to match only the specified
adminandblogpatterns.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: How do you create a blog app in a Django project?
Open a terminal in the project directory containing manage.py and run python manage.py startapp blog. This command creates a new blog directory with its own application structure. The existing manage.py file and main Django project directory remain unchanged. The new app can then contain its own views and URL configuration for the blog section.
Q: What is the difference between a Django project and an app?
A Django project represents the overall website, while an app represents a distinct section or function within that website. For example, one project can contain a blog app and a store app. Separating these sections helps organize the project, and an individual app can also be placed into multiple website projects when its functionality is reusable.
Q: How do you create a basic Django view with HttpResponse?
In the blog app's views.py file, import HttpResponse from django.http. Define a function named home that accepts a request argument, even if that argument is not used yet. Return an HttpResponse containing the desired content, such as Blog Home wrapped in an h1 element, to provide the route's visible output.
Q: How do you connect a Django URL path to a view?
Create urls.py inside the blog app, import path from django.urls, and import the local views module with from . import views. Add a urlpatterns list containing a path with an empty route, views.home as the handler, and blog-home as its name. This connects the blog app's base path to the home view.
Q: Why does the blog homepage use an empty URL path?
The project-level URL configuration already matches the blog/ portion of the address before forwarding the request to the blog app. Therefore, the app-level configuration uses an empty path to represent the blog homepage itself. When a visitor requests /blog, Django forwards the request to blog.urls, where the empty path matches and calls views.home.
Q: How does include work in Django URL routing?
The include function sends part of the URL routing process to another URL configuration. After importing include from django.urls, the project-level configuration maps the blog/ path to include('blog.urls'). A request for /blog is therefore passed into the blog app's URL patterns, where the remaining empty path maps to the home view.
Q: Why should a Django route be named blog-home instead of home?
A specific route name such as blog-home helps prevent naming collisions during reverse lookups. A project can contain several apps, and another app, such as a store, might also define a home route. Including the app's identity in the route name makes it clear which homepage the name represents and distinguishes it from routes belonging to other apps.
Q: Why does the Django website root return a 404 error?
The root address returns a 404 because it does not match any configured URL pattern. The project URL configuration contains patterns for admin and blog, but no pattern for the empty root path. Django checks the available patterns in order and returns a page-not-found response when neither one matches the requested address. Visiting /blog matches the configured blog route instead.
Summary & Key Takeaways
-
A Django project can contain multiple apps, each responsible for a distinct part of the website. The tutorial creates a blog app by running
python manage.py startapp blogfrom the directory containingmanage.py. Django then generates a blog directory with the initial files needed for the new application. -
The blog home view is created in
views.pyas a function namedhomethat accepts the required request argument. It returns anHttpResponsecontainingBlog Homeinside anh1element. This view holds the logic that determines what visitors receive when they reach the blog homepage. -
Routing is divided between the project and the blog app. The project URL configuration forwards the
/blogpath throughinclude('blog.urls'). Insideblog/urls.py, an empty path maps toviews.homeand receives the nameblog-home. Testing/blogdisplays the response, while the unmatched root path returns a 404 error.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Corey Schafer 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator