On 12 nov, 07:07, EmileZola <[email protected]> wrote:
> Alright, I'm pretty new to Django and I've got this problem I can't
> quite solve.
>
> I basically have two models, wich I use to put blog articles into
> categories. One is a Category and the other is a Subject. So in my
> categories I might have ('Programming', 'Music', 'Essays') and my
> subjects are listed under those various categories. So under
> Programming I might have ('Python', 'PHP', 'ASM').
>
> Here a stripped down version of my models :
>
> class Category(models.Model):
> name = models.CharField(_('Categorie'), max_length=100)
>
> class Subject(models.Model):
> name = models.CharField(_('Forum'), max_length=100)
> description = models.CharField(_('Description'), max_length=250)
> category = models.ForeignKey(Category)
>
> Now, what I try to do in my view, is to loop through the categories
> and list each subject it contains. Someone can help me ?
Why do you want to do this in the view ??? If it's for rendering
purpose - which I guess is the case - just pass the appropriate
Category queryset to your context, then do the loop in the template,
ie:
# yourapp/views.py
def category_list(request, ...):
context = dict(categories = Category.objects.all().select_related
())
return render_to_response(
"yourapp/category_list.html",
context,
context_instance=RequestContext(request)
)
# yourapp/category_list.html
<ul>
{% for category in categories %}
<li>
<b>{{ category.name }}</b>
<ul>
{% for subject in category.subject_set.all %}
<li>{{ subject.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
HTH - Or did I miss the point ?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=.