Hi Mohammad,

Check out slice
<https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#slice>.

It's a builtin template filter that can be used like this:

{% for author in book.authors.all|slice:"3" %}
  {# HTML up in here #}
{% endfor %}

Good luck,
Elliot


On Sun, Jun 15, 2014 at 9:31 AM, alghafli <theb...@gmail.com> wrote:

>  Hello django users. I am working on my first django project and I faced a
> little problem.
> I have a library database with books. A book element may have several
> authors.
> In the view function I query the database for books. I want to show the *first
> three* authors. I was thinking to do something similar to the following
> python code but using the template language:
>
> authors_to_show = book.authors.get_queryset() [0:3]
> for author in authors_to_show:
>     #html code here
>
> However, it appears that I cannot define variables in the template unless
> I extend the language a bit. Note that I have several books to process and
> I can do that using the for loop in the template language. But I want to
> ignore some authors in each book.
> Any idea of a proper way to do this.
>
> Thank you in advance,
> Mohammad
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/539DA033.4060100%40gmail.com
> <https://groups.google.com/d/msgid/django-users/539DA033.4060100%40gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAAErR_z4JoZu0Xe7M7LiJq-3M4pR%2BYwTxaEnJk56AqZnX-nZHg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to