T
T
Tash1moto2015-10-07 17:05:36
Django
Tash1moto, 2015-10-07 17:05:36

Is it possible to do a django-like query in jinja2?

Hello everyone, is it possible to perform a similar selection in jinja2 (django)?
Or is it necessary to make a selection in views and then pass it to html ?
I want to do like this:

{% for p in products %}
    <h1>{{ p.product_name }}</h1>
    <h4>{{ colors.filter(s = p.product_name) }}</h4>
{% endfor %}

here are the models:
class Products(models.Model):
    product_name = models.CharField(max_length=50,default='none',verbose_name='Название')
    about = models.CharField(max_length=50,default='none',verbose_name='Информация')
    class Meta:
        verbose_name = "Продукт"
        verbose_name_plural='Продукты'
    def __unicode__(self):
        return self.product_name



class Colors(models.Model):
    s = models.ForeignKey(Products)
    white = models.BooleanField(default=False)
    yellow= models.BooleanField(default=False)
    green= models.BooleanField(default=False)
    class Meta:
        verbose_name_plural='Цвета'
    ### 100+ colors

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2015-10-07
@un1t

You cannot write such code in templates. You would have started writing raw SQL queries here. It is impossible not because the template engine does not allow, but because these are templates - the MVC logic does not allow. Not php anyway.
Take out such code in .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question