Django Blog #15: Adding URL templates to views

by Alex
Django Blog #15: Adding URL templates to views

URL templates allow you to associate URLs with views. A URL template consists of a string template, a view, and a name (optional), which can be used to specify a name for the URL of the whole project. Django goes through each template and stops at the first one that matches the requested URL. The library then imports the representation of the matched URL template and executes it by passing an instance of the HttpRequest class and keyword or positional arguments. Create a file in the blog application folder and add the following lines:

from django.urls import path  
from . import views  
app_name = 'blog' 
urlpatterns = [ 
   # post views 
    path('', views.post_list, name='post_list'),  

In this code, the application namespace has been defined using the app_name variable. This allows you to organize URLs by app and use names referencing them. In the example, two patterns have been defined using path(). The first URL pattern takes no arguments and is bound to the post_list view. The second takes the following 4 arguments and is linked to the post_detail view:

  • year: requires an integer
  • month: requires an integer
  • day: requires an integer
  • post: can be composed of words and hyphens

Angle brackets are used to capture values from the URL. Any value defined in the URL pattern as <parameter> is captured as a string. Path converters, such as <int:year>, are used for exact matches and return integer values. And <slug:post> is needed for link matching (a string consisting of ASCII characters and numbers plus hyphens and underscores). All converters are listed here: If the path() and converters method is not suitable, you can consider re_path(). This is used to define complex URL patterns using Python regular expressions. To learn more about defining URL patterns using regular expressions, go to If you haven’t worked with them before, check out this topic in the HOWTO:

Creating a

file for each application is the best way to make sure they can be used in other projects. Now you need to include the blog application URL templates in the main URL templates of the project. Edit the file in the project’s mysite folder to look like this:

from django.urls import path, include  
from django. contrib admin  
urlpatterns = [ 
    path('blog/', include('blog.urls', namespace='blog')),  

The new URL template defined with include refers to the URL templates defined in the blog application. Thus they refer to the blog/ path and are included under the blog namespace. The namespaces must be unique throughout the project. Blog URLs can later be referenced using namespaces, creating them like blog:post_list or blog:post_detail. For more information on URL namespaces, see

Canonical URLs for models

You can use the post_detail URLs defined in the previous section to build canonical URLs for post objects. According to Django rules, you need to add the get_absolute_url() method to the model. It will return the canonical URL of the object. The reverse() method will be used for this method, which allows you to create URLs according to their names and the passed optional parameters. Edit the file and add the following:

from django.urls import reverse

class Post(models.Model) 
   # ...
   def get_absolute_url(self) 
       return reverse('blog:post_detail',  
		       args=[self.publish. year,  
		       self.publish. month,  
		       self.publish. day,  

Templates will use the get_absolute_url() method to link to specific posts.

Related Posts