簡體   English   中英

模板渲染快捷方式未顯示任何結果

[英]Template Render Shortcuts not showing any result

我試圖將我的views.py上的pymongo變量傳遞給模板。 我沒有收到任何錯誤,但是我的代碼也沒有呈現到模板中。

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)

我的HTML代碼非常簡單。 我只是想打印網址列表:

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

我的結果:

  • URLSSSS

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

當模板中有{{ xy }}時,表示“對象x屬性或字典鍵y ”。

因此,在模板中,當您擁有{{ theURL.theURLs }} ,這意味着“對象theURL屬性或字典鍵theURLs ”。

theURL已經在循環中的每個對象theURLs 這些對象是否真的具有也稱為theURLs屬性? 您的代碼中沒有任何東西表明是這種情況。

好了,長時間查看代碼后,有兩個主要問題。

  1. <li>{{ theURL.theURLs }}</li>應該只是<li>{{ theURL }}</li>
  2. 您的for循環已嚴重混亂。 在當前形式下,您僅采用您迭代的最后一個URL。 以這種方式修復它:

    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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM