[英]Template Render Shortcuts not showing any result
I am trying to pass a variable from pymongo on my views.py to a template. 我试图将我的views.py上的pymongo变量传递给模板。 I am not getting any errors but neither is my code being rendered to my template.
我没有收到任何错误,但是我的代码也没有呈现到模板中。
views.py: views.py:
def getTheA(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'])
theVars = json.dumps(x['tags']['variables'])
context = {'theURLs' : theURLs}
return render(request, 'templates/a.html', context)
My HTML code is pretty simple. 我的HTML代码非常简单。 I am just trying to print a list of the urls:
我只是想打印网址列表:
<ul>
<li><b>URLSSSS</h1></b>
{% for theURL in theURLs %}
<li>{{ theURL.theURLs }} </li>
{% endfor %}
</ul>
My result: 我的结果:
I am new to Django and MongoDb and can't seem to figure out where I went wrong. 我是Django和MongoDb的新手,似乎无法弄清楚哪里出了问题。
When you have {{ xy }}
in a template, that means "attribute or dictionary key y
of object x
". 当模板中有
{{ xy }}
时,表示“对象x
属性或字典键y
”。
So, in your template, when you have {{ theURL.theURLs }}
, that means "attribute or dictionary key theURLs
of object theURL
. 因此,在模板中,当您拥有
{{ theURL.theURLs }}
,这意味着“对象theURL
属性或字典键theURLs
”。
theURL
is already looping over each object in theURLs
. theURL
已经在循环中的每个对象theURLs
。 Do those objects really have attributes also named theURLs
? 这些对象是否真的具有也称为
theURLs
属性? Nothing in your code suggests this is the case. 您的代码中没有任何东西表明是这种情况。
Okay after looking at your code for a long time, there are two main problems. 好了,长时间查看代码后,有两个主要问题。
<li>{{ theURL.theURLs }}</li>
should just be <li>{{ theURL }}</li>
<li>{{ theURL.theURLs }}</li>
应该只是<li>{{ theURL }}</li>
Your for loop is seriously messed up. 您的for循环已严重混乱。 In its current form, you only take the last URL that you iterate over.
在当前形式下,您仅采用您迭代的最后一个URL。 Fix it in this way:
以这种方式修复它:
def getTheA(request): theURLs = [] for yadayadayada: theURLs.append(x['url']) theNames = json.dumps(x['tags']['tag']['name']) theVars = json.dumps(x['tags']['variables']) context = { 'theURLs' : theURLs } return render(request, 'templates/a.html', context=context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.