i want function for post visit on each viewand also show count views in post by id
To achieve this, you can create a function that increments the visit count for each view, and another function that retrieves the count of views for a specific post ID. Here's an example implementation in Python:
# Dictionary to store visit counts for each post
visit_counts = {}
def post_visit(post_id):
if post_id in visit_counts:
visit_counts[post_id] += 1
else:
visit_counts[post_id] = 1
def get_view_count(post_id):
return visit_counts.get(post_id, 0)
Here's how you can use these functions:
# Call post_visit function on each view
post_visit(1)
post_visit(1)
post_visit(2)
post_visit(1)
# Get the view count for post with ID 1
print(get_view_count(1)) # Output: 3
# Get the view count for post with ID 2
print(get_view_count(2)) # Output: 1
In the example above, post_visit function is called on each view to increment the visit count for the corresponding post ID. get_view_count function is used to retrieve the count of views for a specific post ID. If the post ID doesn't exist in the visit_counts dictionary, it returns 0
原文地址: http://www.cveoy.top/t/topic/iVOM 著作权归作者所有。请勿转载和采集!