List Info

Thread: Template Question (for loop)




Template Question (for loop)
user name
2006-07-28 14:51:47
To start off, I have two models:

class Category(models.Model):
    name = models.CharField(maxlength=50)
    visible = models.BooleanField(default=True)

class Forum(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField(maxlength=50)
    visible = models.BooleanField(default=True)

I want to display a list of the forums, grouped by
categories (see
below). If a category is marked invisible, I don't want to
show any
forums, even if they are marked visible. If there are no
forums in a
category, I still want to display the category title.

<h2>category</h2>
<p>forum name</p>
<p>forum name</p>

<h2>category</h2>
<p>no forums in this category</p>

I have my view and template setup like this:

def index(request):
    categories = Category.objects.filter(visible=True)

{% for category in categories %}
    <h2>{{ category.name }}</h2>
    {% if category.forum_set.count %}
        {% for forum in category.forum_set.all %}
            <p>{{ forum.name }}</p>
        {% endfor %}
    {% else %}
        <p>no forums in this category</p>
    {% endif %}
{% endfor %}

This does hide invisible categories, but the problem is that
it shows
all forums, regardless of whether they are marked visible.

Another solution I came up with is to do this in the view:

def index(request):
    forums = Forum.objects.filter(visible=True,
category__visible=True)

Using this method, I can just loop through the forums dict
and use an
{% ifchanged %} to display the category names. The problem
with this is
that I can't display the category name if there are no
forums in it.

Is it possible to do what I want? I'm sure theres a way
(maybe using
the extra() method), I just can't figure it out.I hope I
made my
question clear enough.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Template Question (for loop)
user name
2006-07-28 15:04:50
timster escribió:
> {% for category in categories %}
>     <h2>{{ category.name }}</h2>
>     {% if category.forum_set.count %}
>         {% for forum in category.forum_set.all %}
>             <p>{{ forum.name }}</p>
>         {% endfor %}
>     {% else %}
>         <p>no forums in this category</p>
>     {% endif %}
> {% endfor %}

Why not add some more ifs?

 >         {% for forum in category.forum_set.all %}
+	     {% if forum.visible %}
 >             <p>{{ forum.name }}</p>
+	     {% endif %}
 >         {% endfor %}

Javier.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Template Question (for loop)
user name
2006-07-28 15:21:39
Yuck. I don't want to do it that way. It couples the
display logic with
the template. If I ever need to change the criteria that
determines
when to display forums/categories, I will need to change it
in two
places (view and template).


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Template Question (for loop)
user name
2006-07-28 16:16:22
Could create a list and loop through that, e.g.

cat_list = Category.objects.filter(visible=True)
list = []
for c in cat_list:
   list.append({"cat":c,
"forums":c.forum_set.filter(visible=True,
category__visible=True})

Then:

{% for x in list %}
<h2>{{ c.cat.name }}</h2>
{% if c.forums %}
{% for forum in c.forums %}
...
{% endfor %}
{% else %}
...
{% endif %}
{% endfor %}

Chris


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Template Question (for loop)
user name
2006-07-28 17:17:11
That's not a bad solution.

I just came up with a solution I'm very happy with. I
created a custom
manager that only returns visible objects.

This way I can just do categories = Category.objects.all()
and
category.forum_set.all() in my template and it will only
display
visible objects. It works great 

class VisibleObjectsManager(models.Manager):
    def get_query_set(self):
        return super(VisibleObjectsManager,
self).get_query_set().filter(visible=True)

class Category(models.Model):
    name = models.CharField(maxlength=50)
    visible = models.BooleanField(default=True)
    objects = VisibleObjectsManager()

class Forum(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField(maxlength=50)
    visible = models.BooleanField(default=True)
    objects = VisibleObjectsManager()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Template Question (for loop)
user name
2006-07-29 05:09:53
Well, you beat me to it, but I was going to say that a
cutsom manager
is probably the way I'd do this. 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )