1. Create a CommentForm
in the form.py file it will only contain a single CKEditorField
for users to write their comments.
This is what you're aiming to achieve:
Hint: You might need to check the documentation or day 67 to see how we implement the CKEditor.
The next step is to allow users to leave a comment and save the comment. Now that we've seen how relationships can be established between tables in our database. Let's step up our relationships to create a new Table where any user can write comments to our blog posts.
2. Create a Table called Comment
where the tablename
is "comments"
. It should contain an id
and a text
property which will be the primary key and the text entered into the CKEditor.
3. Establish a One to Many relationship Between the User
Table (Parent) and the Comment
table (Child). Where One User
is linked to Many Comment
objects.
Hint:
4. Establish a One to Many relationship between each BlogPost
object (Parent) and Comment
object (Child). Where each BlogPost
can have many associated Comment
objects.
Hint:
5. We added a new Table added and changed the database schema. Let's re-create our database from scratch once again. Stop your flask server and delete the existing blog.db entirely.
Restart your flask server and register your first user. This will be your admin user.
Create a new blog post:
Create another user: a blog reader. The blog reader will comment on the posts.
6. Only allow registered and logged-in users (users that have been authenticated) to comment on posts. Otherwise, they should see a flash message telling them to log in and redirect them to the /login
route. You will need to update the /post/<int:post_id>
route.
After you've written your code, test it out. Log in as the blog reader, your "John Doe" user (or any user that is not the primary user) and make a comment on a blog post. Check the blog.db to find your comment.
This is what you are aiming for:
If a user is not logged in, clicking "submit comment" should take them back to the login page.
HINT: Will you need a POST method in your routes when adding comments?
7. Our comments are not visible on the page yet! Let's change this. Update the code in post.html to display all the comments associated with the blog post.
This is what you're aiming for:
HINT 1: Don't worry about the commenter image just yet.
HINT 2: comments
is a property of each blog post, you can treat it like a List.
HINT 3: The text of each comment is created from the CKEditor just like the body of each blog post so it will be saved in HTML format.
Gravatar images are used across the internet to provide an avatar image for blog commenters.
e.g. Check out the comments section of this blog post:
Gravatar allows you to change the image that you use across the blog websites that use Gravatar here: https://en.gravatar.com/
It's super simple to implement into a Flask application.
7. Use flask-gravatar to add Gravatar images into your comments section.
This is what you should end up with:
HINT: Check out this page for more detailed info
You can download the completed project in this lesson's resources.