title: How Do I Increment My View Counter in Flask?
body: <p><b>Summary and Background of What I'm Doing:</b></p>
  
  <p><b>I'm creating an app</b> 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). </p>
  
  <p>I'm using <b>MongoDB</b> to host my key/value pairs <b>("views":0)</b> and <b>I want to be able to increment the value of "views" by +1</b> 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 <b>the appropriate way to $inc a value in MongoDB</b>, but I believe I'm missing something from my HTML to properly correspond to that code and I'm not sure what it is. </p>
  
  <p><b>When I run this code</b> 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.</p>
  
  <p>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.</p>
  
  <p><b>What I've Tried So Far:</b></p>
  
  <p>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:</p>
  
  <pre><code>&lt;a href="{{url_for('view_recipe', recipe_id=recipe._id)}}" class="waves-effect waves-light btn btn_small blue"&gt;{{recipe.views})&lt;/a&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p><b>My Code:</b></p>
  
  <p>Here are my routes for both <b>"view_recipe"</b> and <b>"view_count"</b>:</p>
  
  <pre><code>@app.route('/view_recipe/&lt;recipe_id&gt;')
  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/&lt;recipe_id&gt;')
  def view_count(recipe_id):
      mongo.db.recipes
      recipes.update({'_id': str(recipe_id)}, {'$inc': {'views': int(1)}})
      return url_for('view_recipe.html')
  </code></pre>
  
  <p>and here's the section for the view count that I provided in my <b>"view_recipe.html"</b> where I place <b>{{recipe.view}}</b></p>
  
  <pre><code>&lt;a href="{{url_for('view_recipe', recipe_id=recipe._id)}}" class="waves-effect waves-light btn btn_small blue"&gt;{{recipe.views})&lt;/a&gt;
  </code></pre>
  
  <p><b>What Happens When I Run this Code vs What I expect to Happen:</b></p>
  
  <p>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.</p>
  
  <p><b>Just in case...</b></p>
  
  <p>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.</p>
  
  <p>Thank you.</p>