Django Tutorial #5: URLs and Views

Please note that this post may contain affiliate links, and for every purchase you make, at no extra cost to you, a commission fee will be rewarded to me.

We mentioned in the previous sections that all URL declarations are stored in urls.py. Since our plan is to create multiple apps, all URL declarations related to the blog app are in blog/urls.py, and we will include this file in our main urls.py.

urls.py

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

This means if the URL starts with www.yourdomain.com/blog, Django will go to blog/urls.py to look for a match for the rest of the URL.

URLs

After including the blog/urls.py, we can start designing the URL declaration for the blog app.

blog/urls.py

from django.contrib import admin
from django.urls import path
from blog import views


urlpatterns = [
    path('', views.home, name='home'),
    path('category/<slug:slug>', views.category, name='category'),
    path('tag/<slug:slug>', views.tag, name='tag'),
    path('post/<slug:slug>', views.post, name='post'),
]

First, we need to import all necessary functions and packages, including the path function and all the views that we will be creating in this section of the tutorial.

Line 7. If nothing follows www.yourdomain.com/blog, Django will call the home view of our blog app.

Line 8. If the URL is www.yourdomain.com/blog/category/xxx, Django will call the category view, and at the same time, transfer xxx to the view as a variable.

Views

All views related to the blog app are in blog/view.py. You can define them in functions or classes. The class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views.

In this tutorial, we will use function-based views, since it is easier to implement, and it should be enough for a simple blogging system.

Home View

from django.shortcuts import render
from .models import Category, Tag, Post


def home(request):
    posts = Post.objects.all()
    categories = Category.objects.all()
    tags = Tag.objects.all()

    return render(request, 'blog/home.html', {
        'posts': posts,
        'categories': categories,
        'tags': tags,
    })

Line 1-2, import the necessary packages and the required models.

Line 6-8, retrieve the data from the database. all() retrieves all posts, categories and tags from the database.

Line 10-14, return a template along with the retrieved data, using the render() function.

Category View

def category(request, slug):
    posts = Post.objects.filter(category__slug=slug)
    requested_category = Category.objects.get(slug=slug)
    categories = Category.objects.all()
    tags = Tag.objects.all()

    return render(request, 'blog/category.html', {
        'posts': posts,
        'category': requested_category,
        'categories': categories,
        'tags': tags,
    })

Line 2, get all the posts that belong to the category whose slug equals to slug, the variable that was passed from the URL.

Line 3, get the category whose slug equals to slug.

Tag View

def tag(request, slug):
    posts = Post.objects.filter(tag__slug=slug)
    requested_tag = Tag.objects.get(slug=slug)
    categories = Category.objects.all()
    tags = Tag.objects.all()

    return render(request, 'blog/tag.html', {
        'posts': posts,
        'tag': requested_tag,
        'categories': categories,
        'tags': tags,
    })

Post View

def post(request, slug):
    requested_post = Post.objects.get(slug=slug)
    posts = Post.objects.all()
    categories = Category.objects.all()
    tags = Tag.objects.all()

    return render(request, 'blog/post.html', {
        'post': requested_post,
        'posts': posts,
        'categories': categories,
        'tags': tags,
    })

Next Post: Django Tutorial #6: Templates

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *