Executing a request from a template

I am using jinja2 as a template for a django application. I wanted to make a request in a template. I tried to do this:

{% for f in fs %}
  {% Following.objects.filter(follows=f).count() %}
{% endfor %}

I pass "fs" in variables when rendering the templates that are listed.

But it's not right. I cannot make a query_set call because of how my models are defined. Here is a snippet:

class Following(models.Model):
  user = models.ForeignKey(User)
  follows = models.ForeignKey(F)

class F(models.Model):
  name = models.CharField(max_length=50)

So is there a way to do this?

+4
source share
2 answers

If you are using jinja2 and not the regular Django template language, then you should work: you do not say why this is not so.

, , . :

{{ f.following_set.count() }}
+1

, django. , F, .

:

class F(models.Model):
  name = models.CharField(max_length=50)
  def get_follow_count(self):
      Following.objects.filter(follows=self).count()

{% for f in fs %}
  {{ f.get_follow_count }}
{% endfor %}
0

All Articles