Category Navigation
Today, I restructured my SQL database a bit to better handle the displaying of posts based on their categories. Initially, I had a column in the Post table called "category" that was collecting the category (or categories) that I entered in when making a new post. I realized that it would be better to keep each category on a particular post in a separate row in the table - this way I wouldn't need to split the category cell by comma or space in order to add multiple categories to each post.
There is now a new table in my database specifically for categories, with a column that relates that particular category instance to a specific post. It took me a bit of research to figure out how to do an inner join with a filter in order to first query from the 'categories' table based on a particular category, and then reference that category instance with a specific post, but I was able to solve it with a little help from a programming Discord I am in.
I ended up deciding to add a small sidebar on the /blog page of my website with a drop-down containing a unique list of all of the categories, that when clicked brings you to a page with posts tagged with that particular category, and I'm pretty happy with it so far.
Cross-Pollination Contamination
Categories: flask
Today, I pushed the new website code to a github repository, and was immediately rewarded for doing so when a dev friend noticed that I had a few unprotected endpoints in my routes.py file. I didn't realize that it was possible to create HTTP requests through the browser console or other methods, which would have allowed unauthenticated users to perform admin-only tasks on my website.
To resolve this, I simply had to add a g.user if/else statement to the other routes that are utilizing the POST method. Lesson learned: Pay attention to authentication and endpoints before deploying a website (unless you want one of your friends to post the entirety of The Bee Movie script to your blog page.) Shout out to Ian for using his powers for good instead of evil.
🐝
3 Comments:
Barry B. Benson October 24, 2021 |
This sweater is Ralph Lauren, and I have no pants. |
Chuck Staples October 25, 2021 |
Always Bee Careful to secure your endpoints. |
Kelly Pracht November 11, 2021 |
Love the projects! Wonder how many open endpoints I have in past websites! Doh! |
Commenting functionality
Categories: flask
Comments are now functioning on the Blog section of my website. The most difficult part of this was figuring out the best way to relate a comment to a post in the database. Comments have a "post_id" column that is linked to the id of the post it is created under, and I needed to find a way to have this record match up with the related post.
It turns out that jinja can handle this pretty easily within a for loop, and as long as the comment loop is within the post loop you can simply reference the id of the post somewhere in the form you are requesting data from. I used a hidden form and then gave it a value of {{post.id}} (thanks Eben <3) and was able to assign it to a variable in my comments() function and add it as an entry in the database.
<input type="hidden" name="post-id" value="{{post.id">
I gave each comment an "approved" column in the database, which is a boolean that tells me whether or not a comment has been approved. Submitted comments display on my admin page and can be approved or rejected from there - comments that are unapproved are given a value of 0, comments that are approved are given a value of 1, and rejected comments are deleted from the database. I also added a flash message that displays when a user submits a comment to let them know that it is awaiting approval.
I do have a "category" column in the database for blog posts, but I haven't utilized these yet (although I have been adding categories). I will likely display these somewhere under the title at some point in the future.
Next goals:
- Blog sidebar
- Search functionality
- Display by category
0 Comments
Pagination and other functionalities
Categories: flask
Pagination is working for my Blog section, as of yesterday. It was pretty simple due to the paginate method in SQLAlchemy, and I just had to add a few lines of code as well as "Newer posts" and "Older posts" href in the html.
posts = Post.query.order_by(desc(Post.posted_on)).paginate(page, 5, False)
I also have a commenting functionality working in development and hope to deploy it this week. Flask is pretty enjoyable to use once you get over the initial hump of understanding its structure.
1 Comment:
nickybaby October 20, 2021 |
great job here! |
New Flask website and Blog section
Categories: flask
This week, I recreated my website using Python / Flask and added a Blog section. I'll be using this page to write about any projects I'm currently working on as a way of recording my progress as a developer.
Libraries / Methods I used:
- SQLAlchemy for the database
- Flask g for storing data during a request
- Flask session for storing data between sessions
Next goals:
- Improve the design of the Blog section
- Allow comments on the Blog posts
- Add a sidebar with post navigation
- Add pagination
1 Comment:
Calvin October 21, 2021 |
This is a test comment, please ignore. |
0 Comments