EDIT I've resolved this by changing the variable name from post_id to post_num. I'm guessing that the similarity created an ambiguity error somewhere when the argument was passed.
in my post.html HTML:
{% if current_user.is_authenticated %}
{% if current_user.has_liked_post(post) %}
<a href="{{ url_for('users.like_post', post_id=post.id, action='unlike') }}">Unlike</a>
{% else %}
<a href="{{ url_for('users.like_post', post_id=post.id, action='like') }}">Like</a>
{% endif %}
{% endif %}
python routes (both posts and users are registered blueprints):
@users.route('/like/<int:post_id>/<action>')
@login_required
def like_post(post_id, action):
this_id = post_id
post = Post.query.filter_by(id=post_id).first_or_404()
if action == 'like':
current_user.like_post(post)
db.session.commit()
if action == 'unlike':
current_user.unlike_post(post)
db.session.commit()
return redirect(url_for('posts.post', post_id=this_id))
@posts.route("/post/<int:post_id>")
def post(post_id):
post = Post.query.get_or_404(post_id)
print(post)
return render_template('post.html', title=post.title, post=post)
When I click on the "like" button it redirects me to the main index page for some reason. What's more confusing is it seems to be working, because it registers the "likes" in the database correctly, and when I navigate back to the post it displays "like" or "unlike" correctly depending on whether the user has "liked" this post.
Also, when I click on "like" or "unlike" it executes the print(post) function which is inside of the @posts.post route. Meaning the redirect does appear to happening correctly, and it's getting ahold of the correct post_id and printing that information to the python console. However, it then redirects to the main index for some reason rather than staying on/rendering the page for /post/post_id. It also obviously does not redirect to the index page if I access this post/post_id page in any other manner.
I also have the same "return redirect(url_for('posts.post', post_id=this_id))" syntax verbatim in another "update_post" function and this does not seem to have the same issue, it redirects and stays on the page the way it should.
For what it's worth, there's not a single line in my post.html file or any of the lines below that reference the index page.