Adding URL patterns for your views

URL patterns allow you to map URLs to views. A URL pattern is composed of a string pattern, a view, and, optionally, a name that allows you to name the URL project-wide. Django runs through each URL pattern and stops at the first one that matches the requested URL. Then, Django imports the view of the matching URL pattern and executes it, passing an instance of the HttpRequest class and keyword or positional arguments.

Create an urls.py file in the directory of the blog application and add the following lines to it:

from django.urls import pathfrom . import viewsapp_name = 'blog'urlpatterns = [    # post views    path('', views.post_list, name='post_list'), path('<int:year>/<int:month>/<int:day>/<slug:post>/', ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.