Blog Functionality
To make life easier all blog related routes will sit inside their own file (blueprint) and then be registered with the app. This makes it easier to seperate code by functionality rather than having one large file with all routes.
Create a Blueprint
Creating a blueprint is as simple as creating a new file and adding the following code.
With our blueprint created we can now register it with the app. The index
route in our blueprint is also going to be the main page of our website so we will set a url rule to link it to /
.
Routes
For our blog multiple different routes are needed, each serving one of the already created templates. The routes will also need to interact with the database to get the required data to then fill the template.
Index route
Our main page needs to show a preview of all posts and the amount of comments on each post.
-
To only show a preview of the post we use the
SUBSTR
function to only get the first 400 characters of the post body. -
To get the amount of comments on each post we use a
LEFT JOIN
to join thecomment
table to thepost
table. This will return all posts even if they have no comments. We then use thecount
function to count the amount of comments on each post. theLEFT JOIN
command adds the contents of thecomment
table into our query. -
So that all posts are in the correct order we use the
ORDER BY
command to order the posts by their creation date.
Display post
With our index page displaying a preview of all posts we now need to create a page to display the full post and all comments on that post. To keep things organised we will seperate the code to get the post and the code to get the comments into seperate functions. This page will also handle adding comments to the post.
-
The route of
/<int:id>/post
means that the route will be/1/post
for example. Theint
part of the route means that the id will be converted to an integer before being passed to the function. -
As this page has both
GET
andPOST
methods we need to check which method is being used. If the method isPOST
then we know that the user is trying to add a comment to the post. We then get the comment from the form and check that it is at least 10 characters long. If the comment is valid we then add it to the database and redirect the user back to the post page.
Creating posts
Similar to creating comments creating posts functions the same way, when you initially load the page it will be a GET
request and when you submit the form it will be a POST
request. Validation is done to make sure that the title is not empty.
Searching posts
Searching posts is done by using the LIKE
command in SQL. This command allows you to search for a string within a string. The %
character is used as a wildcard so that the search will return any post that contains the search query. Any results are then passed to the template to be displayed.