How to Create a Django App and URL Routes

904.8K views
•
August 31, 2018
by
Corey Schafer
YouTube video player
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 blog in the same directory as manage.py. The command generates a new blog directory with its own structure while leaving the previously created project directory and manage.py unchanged.
  • A Django view is a function that handles traffic for a route and returns what the visitor should see. The home view accepts a request argument and returns an HttpResponse containing the words Blog Home wrapped in an h1 HTML element.
  • An app-level URL configuration is created in a new blog/urls.py file. It imports path from django.urls, imports the local views module, and defines a urlpatterns list 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 /blog prefix. The empty path maps the remaining URL to views.home, causing the blog home response to be returned.
  • The route name blog-home is more specific than a generic name such as home. 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 include from django.urls to forward requests beginning with /blog to blog.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 admin and blog patterns.

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 blog from the directory containing manage.py. Django then generates a blog directory with the initial files needed for the new application.

  • The blog home view is created in views.py as a function named home that accepts the required request argument. It returns an HttpResponse containing Blog Home inside an h1 element. 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 /blog path through include('blog.urls'). Inside blog/urls.py, an empty path maps to views.home and receives the name blog-home. Testing /blog displays the response, while the unmatched root path returns a 404 error.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from Corey Schafer 📚