May 2018
Beginner to intermediate
526 pages
11h 57m
English
Let's start by creating a view to display the list of posts. Edit the views.py file of your blog application and make it look like this:
from django.shortcuts import render, get_object_or_404from .models import Postdef post_list(request): posts = Post.published.all() return render(request, 'blog/post/list.html', {'posts': posts})
You just created your first Django view. The post_list view takes the request object as the only parameter. Remember that this parameter is required by all views. In this view, we are retrieving all the posts with the published status using the published manager we created previously.
Finally, we are using the render() shortcut provided by Django to render the list of posts with the ...
Read now
Unlock full access