如何在 Django ListView 模板的 html 网页中显示字典中的项目?
How can I display items from a dictionary in a html webpage from a Django ListView template?
我正在尝试显示 MongoDB 数据库中的查询结果,但无法显示 views.py
传递的结果。
这是我在 products
:
上用 for 循环迭代显示它们的页面
{% extends "portfolio/base.html" %}
{% load static %}
{% block content %}
<table class="table table-striped" align='center'>
<thead>
<tr>
<th align="center">Brand</th>
<th align="center">Perfume Name</th>
<th align="center">Gender</th>
<th align="center">Theme</th>
<th align="center">Notes</th>
</tr>
</thead>
<tbody>
{% for element in products %}
<tr>
<td>{{ element.q0.Results.0.Brand.Name }}</td>
<td>{{ element.q0.Results.0.Name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
这是 views.py 我请求并发送它们的地方。
import pymongo
import todo.config as config
from django.views.generic import TemplateView, ListView
from django.db.models import Q
username = config.username
password = config.password
client = pymongo.MongoClient(
f"mongodb+srv://{username}:{password}@cluster0.n2hnd.mongodb.net/ifresearch?retryWrites=true&w=majority")
collection = client.test.sephora_backup3
...
class SearchResultsView(ListView):
model = Perfume
template_name = 'todo/search_similar_results.html'
def get_queryset(self): # new
query = self.request.GET.get('q')
print(query)
products = list(collection.find({"q0.Results.0.Name": {"$regex": query, "$options": "i"}}))
print("products: ", products)
return products
产品中有结果,但html
中未显示任何结果
ListView 在您的上下文中使用 get_queryset
的结果设置一个名为 object_list
的变量。此外,如果您设置 context_object_name
它会使用该名称,如果没有,它会尝试使用模型名称(如果 object_list 有它(意味着它是一个查询集)。
在你的例子中,你遍历了 products
但没有这样的上下文变量。你应该设置 context_object_name = 'products'
:
class SearchResultsView(ListView):
model = Perfume
template_name = 'todo/search_similar_results.html'
context_object_name = 'products'
def get_queryset(self): # new
query = self.request.GET.get('q')
print(query)
products = list(collection.find({"q0.Results.0.Name": {"$regex": query, "$options": "i"}}))
print("products: ", products)
return products
我正在尝试显示 MongoDB 数据库中的查询结果,但无法显示 views.py
传递的结果。
这是我在 products
:
{% extends "portfolio/base.html" %}
{% load static %}
{% block content %}
<table class="table table-striped" align='center'>
<thead>
<tr>
<th align="center">Brand</th>
<th align="center">Perfume Name</th>
<th align="center">Gender</th>
<th align="center">Theme</th>
<th align="center">Notes</th>
</tr>
</thead>
<tbody>
{% for element in products %}
<tr>
<td>{{ element.q0.Results.0.Brand.Name }}</td>
<td>{{ element.q0.Results.0.Name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
这是 views.py 我请求并发送它们的地方。
import pymongo
import todo.config as config
from django.views.generic import TemplateView, ListView
from django.db.models import Q
username = config.username
password = config.password
client = pymongo.MongoClient(
f"mongodb+srv://{username}:{password}@cluster0.n2hnd.mongodb.net/ifresearch?retryWrites=true&w=majority")
collection = client.test.sephora_backup3
...
class SearchResultsView(ListView):
model = Perfume
template_name = 'todo/search_similar_results.html'
def get_queryset(self): # new
query = self.request.GET.get('q')
print(query)
products = list(collection.find({"q0.Results.0.Name": {"$regex": query, "$options": "i"}}))
print("products: ", products)
return products
产品中有结果,但html
中未显示任何结果ListView 在您的上下文中使用 get_queryset
的结果设置一个名为 object_list
的变量。此外,如果您设置 context_object_name
它会使用该名称,如果没有,它会尝试使用模型名称(如果 object_list 有它(意味着它是一个查询集)。
在你的例子中,你遍历了 products
但没有这样的上下文变量。你应该设置 context_object_name = 'products'
:
class SearchResultsView(ListView):
model = Perfume
template_name = 'todo/search_similar_results.html'
context_object_name = 'products'
def get_queryset(self): # new
query = self.request.GET.get('q')
print(query)
products = list(collection.find({"q0.Results.0.Name": {"$regex": query, "$options": "i"}}))
print("products: ", products)
return products