I'm trying to check my Mongo database to find a value for the "_id" field with the value returned by the url_for () function from the template. Although I have verified that the value that appears in the URL and the one that passes to the router is correct, the database does not return any matches.
Here is the router code:
from flask import Flask
from flask import render_template
import pymongo
from pymongo import MongoClient
import re
from bson.objectid import ObjectId
try:
dbClient = MongoClient().fabrica.fichas
print("successful connection to fichas")
except:
print('An error occurred on line {} in statement {}'.format(line, text))
exit(1)
app=Flask(__name__)
@app.route('/ficha/<id>')
def ficha(id):
id=ObjectId(id)
print(id)
return render_template('ficha.html', ficha_data=dbClient.find({"_id":ObjectId(id)}))
And this is the template code:
<!doctype html>
<title>Hello from Flask</title>
<h2>Fichas</h2>
<style>th{text-align: left; width: 15%;}</style>
<table id="navigation">
<tr>
<th>Ref</th>
<th>Name</th>
<th>Leather</th>
<th>Color</th>
<th>Client</th>
</tr>
{% for ficha in datos %}
<tr>
<td><a href="{{ url_for('.ficha', id=ficha['_id'])}}">{{ficha['ref']}}</a></td>
<td>{{ficha['name']}}</td>
<td>{{ficha['leather']}}</td>
<td>{{ficha['color']}}</td>
<td><a href="{{ url_for('.fichas_cliente', cliente=ficha['client']) }}">{{ficha['client']}}</a></td>
</tr>
{% endfor %}
</table>
Thank you very much!