Python Forum
How Do I Increment My View Counter in Flask?
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Do I Increment My View Counter in Flask?
#2
Do you have an ajax call, in javascript, that calls /view_count/recipie_id?
I'm just guessing, but it kinda looks like you never call the function that does the incrementing. But if you want it incremented every time the page is refreshed, why not just put the update in the controller?

ie, instead of:
@app.route('/view_recipe/<recipe_id>')
def view_recipe(recipe_id):
    the_recipe =  mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
    return render_template('view_recipe.html', recipe=the_recipe)
 
@app.route('/view_count/<recipe_id>')
def view_count(recipe_id):
    mongo.db.recipes
    recipes.update({'_id': str(recipe_id)}, {'$inc': {'views': int(1)}})
    return url_for('view_recipe.html')
Why not just do:
@app.route('/view_recipe/<recipe_id>')
def view_recipe(recipe_id):
    the_recipe =  mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
    mongo.db.recipes.update({'_id': str(recipe_id)}, {'$inc': {'views': int(1)}})
    return render_template('view_recipe.html', recipe=the_recipe)
Reply


Messages In This Thread
RE: How Do I Increment My View Counter in Flask? - by nilamo - Jan-11-2019, 07:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help in Code for Writing View Functions in Flask - Python Web Framework ashishsme14 2 3,023 May-22-2020, 03:43 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020