My question is related to this question.
I am trying to display all the _id of mongo database in django template from last 2 days but unable to get it.
This is the error:
This is the code I am trying:
views.py
JavaScript
x
13
13
1
register = template.Library()
2
port = 27019
3
client = MongoClient(port=port)
4
mydb = client.catalogDB
5
data = mydb.productCatalog
6
7
def index(request):
8
values = data.find()
9
@register.filter("mongo_id")
10
def mongo_id(value):
11
return str(value['_id'])
12
return render(request, 'product.html', {"values":values})
13
product.html
JavaScript
1
26
26
1
<ul class="row catalog-list">
2
{% for value in values %}
3
<li class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
4
<div>
5
<img src={{value.image_url_medium}}>
6
<label>
7
<input type="checkbox" name="sync-check" class="catalog-checkbox">
8
</label>
9
</div>
10
<div>
11
{% csrf_token %}
12
<h4 class="ellipsis-text catalog-item-name" tooltip={{value.name}}><span class="catalogProductId">{{value.name}}</span></h4>
13
{% load views %}
14
<h5 >Product Id: {{ object|mongo_id }}</h5>
15
<h5>Category:{{value.catagory}}</h5>
16
<h5>Best Price: {{value.best_price}}</h5>
17
<h5>Best Price Vendor: {{value.best_price_vendor}}</h5>
18
<h5 class="ellipsis-text">Link:
19
<a href={{value.best_price_vendor_url}}>{{value.best_price_vendor_url}}</a>
20
</h5>
21
</div>
22
</li>
23
{% endfor %}
24
<h2 class="message">Sorry no records found</h2>
25
</ul>
26
Though, I tried changing the name to static, cache etc, but still no luck.
Advertisement
Answer
template tag written in app/templatetag folder not in view of app.
https://docs.djangoproject.com/en/3.1/howto/custom-template-tags/