所以我有这个mongodb数据,我想循环这个。我正在使用 flask_pymongo,但我不能将 mongodb 数据用作 python 字典。
mongodb数据作为字典列表
[{'_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'}]
到目前为止我的代码
@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'))
所以我将 mongodb 数据转换为字典列表。基本上我想获取所有的键值对,但是每当我遍历数据时,我都会得到第一个这样的字典{'_id': ObjectId('5dea382d9654591293f35747'), 'bookname': 'biology book', 'bookdesc': 'damn son\r\n', 'donatedby': 'iamuser'}
。我想要所有这些。我怎么做 ??
更新代码
get_books.html
我想要的是,我想在我的引导卡中显示那些bookname、donatedby和bookdesc值。所以条件是,如果我的数据库中有用户正在搜索的书。我想显示有关这本书的所有信息。
引导 4 卡
<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>