Enhancing Your Blog with Advanced Features
In the preceding chapter, you created a basic blog application. Next, you will turn your application into a fully functional blog with the advanced functionalities that many blogs feature nowadays. You will implement the following features in your blog:
- Sharing posts via email: When readers like an article, they might want to share it with somebody else. You will implement the functionality to share posts via email.
- Adding comments to a post: Many people want to allow their audience to comment on posts and create discussions. You will let your readers add comments to your blog posts.
- Tagging posts: Tags allow you to categorize content in a non-hierarchical manner, using simple keywords. You will implement a tagging system, which is a very popular feature for blogs.
- Recommending similar posts: Once you have a classification method in place, such as a tagging system, you can use it to provide content recommendations...
Sharing posts by email
First, let's allow users to share posts by sending them via email. Take a minute to think about how you could use views, URLs, and templates to create this functionality using what you learned in the preceding chapter. In order to allow your users to share posts via email, you will need to do the following things:
- Create a form for users to fill in their name, their email, the email recipient, and optional comments
- Create a view in the
views.pyfile that handles the posted data and sends the email
- Add a URL pattern for the new view in the
urls.pyfile of the blog application
- Create a template to display the form
Creating forms with Django
Let's start by building the form to share posts. Django has a built-in forms framework that allows you to create forms in an easy manner. The forms framework makes it simple to define the fields of your form, specify how they have to be displayed, and indicate how they...
Creating a comment system
You will build a comment system wherein users will be able to comment on posts. To build the comment system, you need to do the following:
- Create a model to save comments
- Create a form to submit comments and validate the input data
- Add a view that processes the form and saves a new comment to the database
- Edit the post detail template to display the list of comments and the form to add a new comment
Building a model
First, let's build a model to store comments. Open the
models.py file of your
blog application and add the following code:
class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments') name = models.CharField(max_length=80) email = models.EmailField() body = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField...
Adding the tagging functionality
After implementing your comment system, you need to create a way to tag your posts. You will do this by integrating a third-party Django tagging application into your project.
django-taggit is a reusable application that primarily offers you a
Tag model and a manager to easily add tags to any model. You can take a look at its source code at https://github.com/jazzband/django-taggit.
First, you need to install
pip by running the following command:
pip install django_taggit==1.2.0
Then, open the
settings.py file of the
mysite project and add
taggit to your
INSTALLED_APPS setting, as follows:
INSTALLED_APPS = [ # ... 'blog.apps.BlogConfig', 'taggit', ]
models.py file of your
blog application and add the
TaggableManager manager provided by
django-taggit to the
Post model using the following code:
from taggit.managers import TaggableManager class Post(models.Model...
Retrieving posts by similarity
Now that you have implemented tagging for your blog posts, you can do many interesting things with tags. Tags allow you to categorize posts in a non-hierarchical manner. Posts about similar topics will have several tags in common. You will build a functionality to display similar posts by the number of tags they share. In this way, when a user reads a post, you can suggest to them that they read other related posts.
In order to retrieve similar posts for a specific post, you need to perform the following steps:
- Retrieve all tags for the current post
- Get all posts that are tagged with any of those tags
- Exclude the current post from that list to avoid recommending the same post
- Order the results by the number of tags shared with the current post
- In the case of two or more posts with the same number of tags, recommend the most recent post
- Limit the query to the number of posts you want to recommend
In this chapter, you learned how to work with Django forms and model forms. You created a system to share your site's content by email and created a comment system for your blog. You added tagging to your blog posts, integrating a reusable application, and built complex QuerySets to retrieve objects by similarity.
In the next chapter, you will learn how to create custom template tags and filters. You will also build a custom sitemap and feed for your blog posts, and implement the full text search functionality for your posts.