how to loop through pymongo data?

121 Views Asked by At

so i have this mongodb data and i want to loop over this. i am using flask_pymongo, but i can't use mongodb data as python dictonaries.

mongodb data as list of dictinary

[{'_id': ObjectId('5d7fa9f7d760eb306123be52'), 'bookname': 'biology book', 'bookdesc': 'my bio book', 'donatedby': 'rajesh gupta'}, {'_id': ObjectId('5d7facb59ee3d15c54b059e4'), 'bookname': 'biology book', 'bookdesc': 'my bio book', 'donatedby': 'rajesh gupta'}, {'_id': ObjectId('5dea368b9654591293f35745'), 'bookname': 'biology book', 'bookdesc': 'its my biology book', 'donatedby': 'iamuser'}, {'_id': ObjectId('5dea382d9654591293f35746'), 'bookname': 'biology book', 'bookdesc': 'damn son\r\n', 'donatedby': 'iamuser'}, {'_id': ObjectId('5dea382d9654591293f35747'), 'bookname': 'biology book', 'bookdesc': 'damn son\r\n', 'donatedby': 'iamuser'}]

my code so far

@app.route('/request books', methods=['GET', 'POST'])
@app.route('/get books', methods=['GET', 'POST'])
def requesting_for_books():
    form = RequestBooks()
    if 'email' in session and request.method == 'GET':
        return render_template('get_books.html', title="Get Books", donated_by=session['username'], form=form)
    elif 'email' in session and request.method == 'POST':
        requesting = mongo.db.mylogin
        Requesting_books = requesting.find({'bookname': request.form['bookname']})
        x  = []
        for i in Requesting_books:
            x.append(i)
            real_data = x
            for j in x:
                h = j
        if Requesting_books == None:
            flash("Please enter a search query !")
            return render_template('get_books.html', title='Get Books', form=form)
        else:
            return render_template('get_books.html', title="Get Books", x=x, h=h, form=form)
    else:
        flash('Please Login/Sign Up first to Search for books !')
        return redirect(url_for('home'))

so i am converting mongodb data into list of dictionaries. Basically i want to grab all of the key and value pairs, but whenever i loop over the data i am getting the first dictionary like this {'_id': ObjectId('5dea382d9654591293f35747'), 'bookname': 'biology book', 'bookdesc': 'damn son\r\n', 'donatedby': 'iamuser'} . i want all of them. How do i do that ??

Updated Code

get_books.html

what i want is, i want to show those bookname's, donatedby and bookdesc values in my bootstrap card's. so the condition is, if my database has the book which user is searching for. i want to show all the information about the book.

bootstrap 4 card

<div class="card-deck">
            <div class="card mb-3" style="width:24rem; max-width: 540px;">
                <div class="row no-gutters">
                    <div class="col-md-4">
                        <img src="{{ url_for('static', filename='dist/img/book.svg') }}" alt="..." class="card-img img-responsive">
                    </div>
                    <div class="col-md-8">
                        <div class="card-body">
                            <h5 class="card-title">Biology Book</h5>
                            <p id="my-text" style="font-size:17px;">its a biology book.</p>
                            <p class="text-muted" style="font-size:17px;"">Donted By: Kvatsalay</p>
                    <a href=" #" class="btn btn-outline-success btn-pill ml-2"><i class="far fa-paper-plane"></i> Request</a>
                        </div>
                    </div>
                </div>
            </div>

0

There are 0 best solutions below