How to create a simple blog using Django 3 - part 6

/ #Django


In this part of the series we'll create the views for the frontpage of our blog and the detail view of a blog post.

The frontpage view

Our database is created and has data, some of our templates are ready, now it's time for the views. This is where we pull data from the database and tell the template what to use.

Open up "blog/views.py" in your editor and edit it so it looks like this:

# Import

from django.shortcuts import render

from blog.models import Post

# Views

def frontpage(request):
	posts = Post.objects.filter(status=Post.PUBLISHED)
	
	context = {
		'posts': posts
	}
	
	return render(request, 'templates/index.html', context)

First, we import a function from Django called "render". This is helps us generate HTML by sending it the request, our template file and the data to put in the template. We also import the Post model we create a couple of parts back.

When we've imported the things we need, we can start createing the views. The first view we're creating is the view for the frontpage. We want all the posts with status equal to "published". "Post.PUBLISHED" is one of those properties we added when we created the Post model.

We create a dictionary called "context" to hold all of the information for the template.

Last but not least, we return the generated data from the "render" shortcut.

The post detail view

We're going to need one more view for the post detail page. Add this code below the "frontpage" view.

def post_detail(request, post_slug):
	post = Post.objects.get(slug=post_slug)
	
	context = {
		'post': post
	}
	
	return render(request, 'blog/templates/detail.html', context)

As you can see, it's actually very similar to the frontpage view. But instead of getting all of the published posts, we just want the one with slug equal to the post_slug parameter. In a couple of seconds, I'll show you how this is passed in.

The URLS

Now that the views are ready, we can create the two url's. Open up 'myblog/urls.py' in your editor. Change the whole file to look like this:

# Import
from django.conf.urls import url
from django.contrib import admin

from blog.views import frontpage, post_detail

# Url patterns

urlpatterns = [
  path('admin/', admin.site.urls),
  
  path('', frontpage, name='frontpage'),
  path('<str:post_slug/', post_detail, name='post_detail'),
]

The two first lines are just default things that Django needs. Below, we import the to views we just created.

Inside the urlpatterns list, we add two more paths. '' is the url for the frontpage, we pass in the "frontpage" view and give it a name.

The path for post_detail view is a little bit more complicated. This is a dynamic route which requires a parameter called "post_slug" and it needs to be an "str" (string).

If you remember from the previous post where we created the templates, we used a code looking like this: {% url 'post_detail' post.slug %}. post_detail in this code refers to the name in the path, and post.slug is the paramater we give it.

Let's try it

Everything should be ready now to try. Go to your command line and run the webserver. Open up your browser and go to the front page. Now you should see a list of the blog posts we added in one of the previous posts. You should also be able to click "read more" and see the detail page of the post.

Summary

We actually have a fully working blog now. But there are lot's of different things we need to improve. In the next part of this series, we'll create the views for the category pages and we'll add the categories to our sidebars.

How to create a simple blog using Django 3 series

Part 1 - Installation
Part 2 - File structure
Part 3 - Database models
Part 4 - Admin interface
Part 5 - Creating some templates
Part 6 - Views and URLs
Part 7 - Category views
Part 8 - Media files
Part 9 - Comments
Part 10 - Search


Comments

No comments yet...

Add comment

Subscribe to my newsletter