Визуализация шаблона не передает агрегатную переменную pymongo в шаблон - PullRequest
0 голосов
/ 19 сентября 2018

Я пытаюсь передать переменную из pymongo в файле views.py в шаблон.Я не получаю никаких ошибок, но мой код не отображается в моем шаблоне.

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-код довольно прост.Я просто пытаюсь напечатать список URL:

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

Мой результат:

  • URLSSSS
  • {% для theURLв theURLs%} {theURL.theURLs} {% endfor%}

Я новичок в Django и MongoDb и не могу понять, где я ошибся.

1 Ответ

0 голосов
/ 20 сентября 2018

Обрезая это до того, что вы ищете в данный момент (и исправляя некоторый синтаксис в вашем шаблоне), попробуйте понимание списка:

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>
...