Calling a method from template

I seem to have a problem with calling a method from a django template. Given the model:

class Gallery(models.Model):
    class Meta:
        verbose_name_plural = "Galerie"

    def photo_count(self):
        return self.photo_set.count()

    def random_image(self):
        return self.photo_set.get(id=random.randint(1,self.photo_count()))

    title = models.CharField(max_length=50)
    imgur_id = models.CharField(max_length=15)
    date_created = models.DateTimeField('Data utworzenia', auto_now=True)
    is_active = models.BooleanField()

    def __unicode__(self):
        return self.title

where Gallery is foreignKey for Photo views.py:

def index(request):
    galleries = Gallery.objects.get(is_active=True)
    if galleries.count() is 0:
        messages.warning(request, "Niestety, żadna galeria nie jest aktywna, zaglądnij niebawem!")
        return redirect(reverse(home))
    elif galleries.count() is 1:
        return render(request, 'gallery/home.html', {'gallery': galleries})
    else:
         return render(request, 'gallery/index.html', {'galleries': galleries})

I want to do this in template:

{% for gallery in galleries %}
         {{ gallery.random_image }} <br />
{% endfor %}

The result I get is:

[ Photo object ]
[]
[]
[]

But when i call

{% for gallery in galleries %}
    {{ gallery.photo_count }} <br />
{% endfor %}

The result is correct.

Why does this not work?

Answers


In my opinion you are complicated a little bit.

class Gallery(models.Model):
    class Meta:
        verbose_name_plural = "Galerie"

    def random_image(self):
        return self.photo_set.order_by('?')[0] if self.photo_set.count() else None

    title = models.CharField(max_length=50)
    imgur_id = models.CharField(max_length=15)
    date_created = models.DateTimeField('Data utworzenia', auto_now=True)
    is_active = models.BooleanField()

    def __unicode__(self):
        return self.title

and in the template,

{% for gallery in galleries %}
    {% if gallery.random_image %}
        {{ gallery.random_image }} <br />
    {% else %}
        No images found.
    {% endif %}
{% endfor %}

Read more on order_by here

Note that ? could prove slightly heavy. If so, get thelist of ids of the photo_set associated with this gallery object, and get a random id from this list, rather than doing a random.randint(1,self.photo_count())


Add @property to your fonction.

@property
def photo_count(self):
    return self.photo_set.count()

Need Your Help

key_down event of DateTimePicker

c# datetimepicker keydown

I am using this DateTimePicker control in my UI.

Variable-length form submissions

ruby-on-rails ruby-on-rails-3 form-submit

I'd like to create a form_for statement which allows the user to select elements one-by-one from a drop down menu (or an auto-complete field). As the user selects each item, they should be displaye...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.