D
D
DanOK152021-08-12 19:09:11
MongoDB
DanOK15, 2021-08-12 19:09:11

How to output "title" from mongodb collection to url variable?

We need it to pull the title and insert it into the url variable.
gives it

sorry it's so complicated
thanks in advance

app.py

from flask import Flask, render_template
from flask_pymongo import PyMongo
from pymongo import MongoClient
import os
import random

app = Flask(__name__)

client = MongoClient('mongodb', 27017)
db = client.test

def add_many():
    db.images.insertMany([
        {'_id': 1, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F0.gif?alt=media&token=0fff4b31-b3d8-44fb-be39-723f040e57fb"},
        {'_id': 2, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F1.gif?alt=media&token=2328c855-572f-4a10-af8c-23a6e1db574c"},
        {'_id': 3, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F10.gif?alt=media&token=647fd422-c8d1-4879-af3e-fea695da79b2"},
        {'_id': 4, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F11.gif?alt=media&token=900cce1f-55c0-4e02-80c6-ee587d1e9b6e"},
        {'_id': 5, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F2.gif?alt=media&token=8a108bd4-8dfc-4dbc-9b8c-0db0e626f65b"},
        {'_id': 6, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F3.gif?alt=media&token=4e270d85-0be3-4048-99bd-696ece8070ea"},
        {'_id': 7, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F4.gif?alt=media&token=e7daf297-e615-4dfc-aa19-bee959204774"},
        {'_id': 8, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F5.gif?alt=media&token=a8e472e6-94da-45f9-aab8-d51ec499e5ed"},
        {'_id': 9, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F7.gif?alt=media&token=9e449089-9f94-4002-a92a-3e44c6bd18a9"},
        {'_id': 10, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F8.gif?alt=media&token=80a48714-7aaa-45fa-a36b-a7653dc3292b"},
        {'_id': 11, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F9.gif?alt=media&token=a57a1c71-a8af-4170-8fee-bfe11809f0b3"},
        ])


@app.route("/cats/<id>")
def index(id):
    url = db.images.find({"_id": id}, {"title": 1})
    return render_template("index.html", url=url)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), debug=True)


index.html-
<html>
  <head>
    <style type="text/css">
      body {
        background: black;
        color: white;
      }
      div.container {
        max-width: 500px;
        margin: 100px auto;
        border: 20px solid white;
        padding: 10px;
        text-align: center;
      }
      h4 {
        text-transform: uppercase;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h4>Cat Gif of the day</h4>
      <img src="{{url}}" />
    </div>
  </body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cython, 2021-08-13
@DanOK15

The problem is that you were passing id to search as a string, not as a number.
I also do not recommend the field itself to override _id, it is better to select another field for this, and name the variables id in python, since you override the standard id function with this.

from flask import Flask, render_template
from flask_pymongo import PyMongo
from pymongo import MongoClient
import os
import random

app = Flask(__name__)

client = MongoClient('mongodb', 27017)
db = client.test

def add_many():
    db.images.insert_many([
        {'id': 1, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F0.gif?alt=media&token=0fff4b31-b3d8-44fb-be39-723f040e57fb"},
        {'id': 2, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F1.gif?alt=media&token=2328c855-572f-4a10-af8c-23a6e1db574c"},
        {'id': 3, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F10.gif?alt=media&token=647fd422-c8d1-4879-af3e-fea695da79b2"},
        {'id': 4, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F11.gif?alt=media&token=900cce1f-55c0-4e02-80c6-ee587d1e9b6e"},
        {'id': 5, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F2.gif?alt=media&token=8a108bd4-8dfc-4dbc-9b8c-0db0e626f65b"},
        {'id': 6, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F3.gif?alt=media&token=4e270d85-0be3-4048-99bd-696ece8070ea"},
        {'id': 7, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F4.gif?alt=media&token=e7daf297-e615-4dfc-aa19-bee959204774"},
        {'id': 8, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F5.gif?alt=media&token=a8e472e6-94da-45f9-aab8-d51ec499e5ed"},
        {'id': 9, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F7.gif?alt=media&token=9e449089-9f94-4002-a92a-3e44c6bd18a9"},
        {'id': 10, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F8.gif?alt=media&token=80a48714-7aaa-45fa-a36b-a7653dc3292b"},
        {'id': 11, 'title': "https://firebasestorage.googleapis.com/v0/b/docker-curriculum.appspot.com/o/catnip%2F9.gif?alt=media&token=a57a1c71-a8af-4170-8fee-bfe11809f0b3"},
        ])


@app.route("/cats/<int:image_id>")
def index(image_id):
    result = db.images.find_one({"id": image_id})
    if result:
        return render_template("index.html", url=result["title"])
    else:
        return "Не найдено"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), debug=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question