G
G
Georgy Baruchyan2016-11-20 01:59:43
Django
Georgy Baruchyan, 2016-11-20 01:59:43

How to display a dictionary element in a template?

Good night everyone.
The essence of the question is this:
I pass the items object to the template. Elements have id, title, link fields.
Also, I pass a counts dictionary with the following content {8: 136, 1: 6, 18: 2, 19: 1, 17: 1}
Counts a dictionary of element counters by category
Items - categories
I need to display in the template Category and the number of items in it The
question is how do I request the element counts from item.id ??
Tried: counts.item.id, counts[item.id], {{counts.{{item.id}}}} - all result is the same - server returns 502
error
, if I do {{counts.8}}

{% if items %}
    {% for item in items %}
        <p><a href="/video/{{ item.link }}">{{ item.title }}</a> — {{ counts }}</p>
    {% endfor %}
{% endif %}

models code
from django.db import models
from persons.models import PersonsItem
from django.utils import timezone
import datetime


class VideoTags(models.Model):
    tag = models.CharField(max_length = 100,verbose_name = 'Тэг')

    def __str__(self):
        return self.tag

    class Meta():
        verbose_name = 'Тэг'
        verbose_name_plural = 'Тэги'
        db_table = 'video_tags'



class VideoCategories(models.Model):
    title = models.CharField(max_length=50, verbose_name = 'Название')
    link = models.CharField(max_length=50, verbose_name = 'Ссылка')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'
        db_table = 'video_categories'

class VideoItems(models.Model):
    title = models.CharField(max_length=50, verbose_name='Название')
    link = models.CharField(max_length=50, verbose_name='Ссылка')
    youtube_link = models.CharField(max_length=50, verbose_name='Ссылка Youtube')
    category = models.ForeignKey(VideoCategories, verbose_name= 'Категория')
    publ = models.BooleanField(verbose_name = 'Публикация', default=True)
    text = models.TextField(verbose_name = 'Описание',default='')
    publish_date = models.DateTimeField(verbose_name='Дата публикации', auto_now_add = True)
    tags = models.ManyToManyField(VideoTags, verbose_name = 'Тэги')
    uch = models.CharField(max_length=50)
    old_tags = models.CharField(max_length=50,default='')
    operator = models.ForeignKey(PersonsItem,related_name='oper',verbose_name='Оператор',on_delete=models.CASCADE)
    director = models.ForeignKey(PersonsItem,related_name='dir',verbose_name='Ружиссёр',on_delete=models.CASCADE)
    
    dateu = models.IntegerField(default=0)


    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Видео'
        verbose_name_plural = 'Видео'
        db_table = 'video_items'

template_tags
from django import template

from django.http import HttpRequest
from django.template.loader import get_template
from persons.models import PersonsItem
from video.models import VideoCategories, VideoItems

videos = VideoItems.objects.filter(publ = True).order_by('-publish_date')

register = template.Library()
templates_dir = 'tags/'


def video_categories():
    counts = {}
    items = VideoCategories.objects.order_by('title')
    if videos:
        for video in videos:
            if not video.category_id in counts:
                counts[video.category_id] = 0
            counts[video.category_id] += 1
    return {
        'items': items,
        'counts': counts,
    }
register.inclusion_tag(templates_dir+'video_categories.html')(video_categories)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-11-20
@sim3x

{% for key, value in items.items %} 
   {{key}} {{value}}
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question