python - get mongodb "_id" in django template -
my question related this question.
i trying display _id of mongo database in django template last 2 days unable it.
this error:
this code trying:
views.py
register = template.library() port = 27019 client = mongoclient(port=port) mydb = client.catalogdb data = mydb.productcatalog def index(request): values = data.find() @register.filter("mongo_id") def mongo_id(value): return str(value['_id']) return render(request, 'product.html', {"values":values}) product.html
<ul class="row catalog-list"> {% value in values %} <li class="col-xs-12 col-sm-6 col-md-4 col-lg-4"> <div> <img src={{value.image_url_medium}}> <label> <input type="checkbox" name="sync-check" class="catalog-checkbox"> </label> </div> <div> {% csrf_token %} <h4 class="ellipsis-text catalog-item-name" tooltip={{value.name}}><span class="catalogproductid">{{value.name}}</span></h4> {% load views %} <h5 >product id: {{ object|mongo_id }}</h5> <h5>category:{{value.catagory}}</h5> <h5>best price: {{value.best_price}}</h5> <h5>best price vendor: {{value.best_price_vendor}}</h5> <h5 class="ellipsis-text">link: <a href={{value.best_price_vendor_url}}>{{value.best_price_vendor_url}}</a> </h5> </div> </li> {% endfor %} <h2 class="message">sorry no records found</h2> </ul> though, tried changing name static, cache etc, still no luck.
template tag written in app/templatetag folder not in view of app.
https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/

Comments
Post a Comment