Bottom Page

Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Do I Increment My View Counter in Flask?
#1
Summary and Background of What I'm Doing:

I'm creating an app which allows you to add and store recipes and I would like to be able to organise my recipe list using the numbers of views each recipe receives. I should explain that I have a list of recipes and if you click on the title of one of the recipes in that list, it will bring you to a new page and display that recipe. This way I can organise my app into three categories of lists (Breakfast, Lunch, and Dinner).

I'm using MongoDB to host my key/value pairs ("views":0) and I want to be able to increment the value of "views" by +1 each time someone refreshes the page. I'll provide my code below, but I think my problem is that I can't seem to make the value increment in my HTML or in MongoDB. Let me explain: I'm almost certain that my routing code is correct. I've watched many tutorials on the appropriate way to $inc a value in MongoDB, but I believe I'm missing something from my HTML to properly correspond to that code and I'm not sure what it is.

When I run this code it still works and there's no error warning, but when I get to any recipe page, the view counter doesn't change when I refresh that page. It just remains at 0.

I should also say that I have two separate routes which I think relate to each other in this case: I have a "view_recipe" route which shows the page of the recipe, and I have a "view_count" route which functions to count views. I'll provide both of these below along with my HTML for everyone to inspect.

What I've Tried So Far:

For my recipe app I've followed Youtube tutorials on how to $inc my key/value, however it's the information that I need to provide to my HTML which I'm unsure of. Based on other examples, my corresponding view information in HTML is this:

<a href="{{url_for('view_recipe', recipe_id=recipe._id)}}" class="waves-effect waves-light btn btn_small blue">{{recipe.views})</a>

I've provided a url_for the route and made what I thought was the relevant recipe_id=recipe._id like I saw in other examples I've used to target the id.

My Code:

Here are my routes for both "view_recipe" and "view_count":

@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')
and here's the section for the view count that I provided in my "view_recipe.html" where I place {{recipe.view}}

<a href="{{url_for('view_recipe', recipe_id=recipe._id)}}" class="waves-effect waves-light btn btn_small blue">{{recipe.views})</a>

What Happens When I Run this Code vs What I expect to Happen:

The code runs, however it doesn't increment like I expect it to. The counter just stays at 0 and doesn't go up when I refresh the page. My hope is that once the page is refreshed, the value of the counter will, increment and be displayed in both the browser and update in my MongoDb collection.

Just in case...

If you need any more information or if I haven't been clear enough or explain this well, please let me know and I'll fix it.

Thank you.
Quote
#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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How one should understand what Flask-WTF is? dukoolsharma 2 304 Nov-24-2018, 06:30 AM
Last Post: himanibansal
  Need help with fixing iteration counter Kapolt 8 443 Oct-18-2018, 07:56 PM
Last Post: buran
  Python Counter - Convert to a Class jill1978 1 279 Oct-16-2018, 10:52 AM
Last Post: Larz60+
  newbie in python. odd ,even & Zero counter. output not crrect khan001 2 651 Dec-06-2017, 04:56 PM
Last Post: gruntfutuk
  How do you compute tf-idf from a list without using the counter class syntaxkiller 8 980 Dec-01-2017, 05:24 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)