模板渲染没有将 pymongo 聚合变量传递给模板 [英] Template Render is not passing pymongo aggregate variable to template
本文介绍了模板渲染没有将 pymongo 聚合变量传递给模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 views.py 上的 pymongo 变量传递给模板.我没有收到任何错误,但我的代码也没有呈现到我的模板中.
I am trying to pass a variable from pymongo on my views.py to a template. I am not getting any errors but neither is my code being rendered to my template.
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 代码非常简单.我只是想打印一个网址列表:
My HTML code is pretty simple. I am just trying to print a list of the urls:
<ul>
<li><h1>URLSSSS</h1></li>
{% for theURL in theURLs %}
<li>{ theURL.theURLs }
{% endfor %}
</ul>
我的结果:
- URLSSSS
- {% for theURL in theURLs %}{ theURL.theURLs } {% endfor %}
我是 Django 和 MongoDb 的新手,似乎无法弄清楚我哪里出错了.
I am new to Django and MongoDb and can't seem to figure out where I went wrong.
推荐答案
将其缩减为您此时正在寻找的内容(并更正模板中的某些语法),尝试列表理解:
Trimming this down to just what you're looking for at this point (and correcting some syntax in your template), try a list comprehension:
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})
模板/a.html:
<ul>
<li><h1>URLSSSS</h1></li>
{% for theURL in theURLs %}
<li>{{ theURL }}</li>
{% endfor %}
</ul>
这篇关于模板渲染没有将 pymongo 聚合变量传递给模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文