模板渲染不会将 pymongo 聚合变量传递给模板



我正在尝试将一个变量从我的 views.py 上的pymongo传递给模板。我没有收到任何错误,但我的代码也没有呈现到我的模板。

views.py:

def gettheAudit(request):
for x in mycol.aggregate([{"$unwind":"$tags"},{'$match': {'tags.tag.name':'A A',}},{'$project': {'url': 1, 'AR': 1, 'tags.tag.name': 1, 'tags.variables': 1, '_id': 0}},]):
theURLs = x['url']
theNames = json.dumps(x['tags']['tag']['name'])
theVs = json.dumps(x['tags']['variables'])
template = loader.get_template('templates/a.html')
context = {
'theURLs' : theURLs,
'theNames' : theNames,
'theVs' : theVs,       
}
return HttpResponse(template.render(context, request))

我的HTML代码非常简单。我只是想打印一个网址列表:

<ul>
<li><h1>URLSSSS</h1></li>
{% for theURL in theURLs %}
<li>{ theURL.theURLs }
{% endfor %}
</ul>

我的结果:

  • 网址
  • {% for url in theURL %} { theURL.theURLs } {% endfor %}

我是Django和MongoDb的新手,似乎无法弄清楚我哪里出错了。

将其修剪为此时要查找的内容(并更正模板中的一些语法(,请尝试列表理解:

from django.shortcuts import render
def gettheAudit(request):
theURLs = [x for x in mycol.aggregate([{"$unwind":"$tags"},{'$match': {'tags.tag.name':'A A',}},{'$project': {'url': 1, 'AR': 1, 'tags.tag.name': 1, 'tags.variables': 1, '_id': 0}},])]
return render(request, 'templates/a.html', {'theURLs': theURLs})

templates/a.html:

<ul>
<li><h1>URLSSSS</h1></li>
{% for theURL in theURLs %}
<li>{{ theURL }}</li>
{% endfor %}
</ul>

最新更新