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 path
from . import views

app_name = 'blog'

urlpatterns = [
# post views
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
]

In the preceding code, we define an application namespace with the app_name variable. This allows us to organize URLs by application and use the name when referring to them. We define two different patterns using the path() function. The first URL pattern doesn't take any arguments and is mapped to the post_list view. The second pattern takes the following four arguments and is mapped to the post_detail view:

  • year: Requires an integer
  • monthRequires an integer
  • dayRequires an integer
  • post: Can be composed of words and hyphens

We use angle brackets to capture the values from the URL. Any value specified in the URL pattern as <parameter> is captured as a string. We use path converters, such as <int:year>, to specifically match and return an integer and <slug:post> to specifically match a slug (a string consisting of ASCII letters or numbers, plus the hyphen and underscore characters). You can see all path converters provided by Django at https://docs.djangoproject.com/en/2.0/topics/http/urls/#path-converters.

If using path() and converters isn't sufficient for you, you can use re_path() instead to define complex URL patterns with Python regular expressions. You can learn more about defining URL patterns with regular expressions at https://docs.djangoproject.com/en/2.0/ref/urls/#django.urls.re_pathIf you haven't worked with regular expressions before, you might want to take a look at the Regular Expression HOWTO located at  https://docs.python.org/3/howto/regex.html first.

Creating a urls.py file for each app is the best way to make your applications reusable by other projects.

Now, you have to include the URL patterns of the blog application in the main URL patterns of the project. Edit the urls.py file located in the mysite directory of your project and make it look like the following:

from django.urls import path, include
from django.contrib import admin

urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
]

The new URL pattern defined with include refers to the URL patterns defined in the blog application so that they are included under the blog/ path. We include these patterns under the namespace blog. Namespaces have to be unique across your entire project. Later, we will refer to our blog URLs easily by including the namespace, building them, for example, blog:post_list and blog:post_detail. You can learn more about URL namespaces at https://docs.djangoproject.com/en/2.0/topics/http/urls/#url-namespaces.