Reputation: 8799
I have 2 models:
class Review(models.Model):
user = models.ForeignKey(User, related_name="user_blog")
tag = TaggableManager()
product = models.ForeignKey(Product)
review_text = models.TextField()
created = models.DateTimeField(auto_now=True, auto_now_add=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=True)
hashtag = models.ForeignKey(Hashtag)
class Product(models.Model):
name = models.CharField(max_length=500)
#inserir campo imagem
#inserir slugify na url do produto
url = models.SlugField(max_length=500)
category = models.ForeignKey(Category)
image = models.ImageField(upload_to='thumbs/')
created = models.DateTimeField(auto_now=True, auto_now_add=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=True)
I want to parse into a template all tags related to the product, product name and image. The template is like this:
<li class="span3">
<div class="product-box">
<img src="{{ STATIC_URL }}{{PRODUCT IMAGE}}" alt="" /></a></p>
<a href="product_detail.html" class="title">PRODUCT NAME</a><br/>
<a href="products.html" class="category">PRODUCT TAG</a>
<p class="price">$17.25</p>
</div>
In my view I'm doing like this:
def home(request):
if request.user.is_authenticated():
user = request.user
prods = Product.objects.all()
i = 0
prodmatrix = {}
for prod in prods:
tags = Review.objects.filter(product=prod.id)
for tag in tags:
prodmatrix[str(i)] = [[prod.name], [prod.image], [tag]]
i = i + 1
return render(request, 'home.html',{'prodmatrix':prodmatrix})
My problem is that in the template, I'm just getting this: [[U'KINECT XBOX 360'], [], []] when I use a for loop like this:
{% for key, value in prodmatrix.items %}
The other problem is that, the product can have infinite tags, but I just want to parse 3 of them, so some produts will have 0,1,2 or 3 tags. How can i do this?
UPDATE:
This is how I'm doing:
for prod in prods:
# 0 1 2
prodmatrix[str(i)] = [[prod.name], [prod.image], []]
review = Review.objects.get(product=prod.id) # ^ this is for tags
for tags in review.tag.all(): #
prodmatrix[str(i)][2].append(tags.name) # append only tags
i = i + 1
And I'm getting:
KeyError at /
'1'
Upvotes: 1
Views: 99
Reputation: 58291
Correct code, you are over-wiring same dict[key] position in loop as prodmatrix[str(i)] = [[prod.name], [prod.image], [tag]]
, you should append append:
first way:
for prod in prods:
prodmatrix[str(i)] = []
tags = Review.objects.filter(product=prod.id)
for tag in tags:
prodmatrix[str(i)].append([[prod.name], [prod.image], [tag]])
i = i + 1
second way:
Or to just append new tags do as (I feel you need this one, same name and image for tags for one prod.id):
for prod in prods:
# 0 1 2
prodmatrix[str(i)] = [[prod.name], [prod.image], []]
tags = Review.objects.filter(product=prod.id) # ^ this is for tags
for tag in tags: #
prodmatrix[str(i)][2].append(tag) # append only tags
i = i + 1
Suppose if you do as in second way then in template you need nested loop:
{% for key, values in prodmatrix.items %}
{% for tag in values.2 %} # or if you wants just first three
# values.2.0, values.2.1 values.2.2
Upvotes: 1