Hey Daniel, First of all thanks a lot for your response. You understood it right that i just want to split up the columns by the blog id so the defintion of blogs_one and blogs_two was really helpful, thanks. So in the shell i found out the blogs_x sort the posts. But unfortunately i can't get it straight to let this work in the template. I worked through the http://docs.djangoproject.com/en/dev/ref/templates/builtins/ page and used several conditioners (for / if ) which all don't work or I applied them wrong. For now I have this
************************** views.py ********************* def blogs(request, username=None, template_name="blog/blogs.html"): blogs_one = Post.objects.filter(blog__id=1) global blogs_one blogs_two = Post.objects.filter(blog__id=2) global blogs_two if username is not None: user = get_object_or_404(User, username=username.lower()) blogs_one = Post.objects.filter(author=user, blog__id=1) blogs_two = Post.objects.filter(author=user, blog__id=2) return render_to_response(template_name, { "blogs_one": blogs_one, "blogs_two": blogs_two, }, context_instance=RequestContext(request)) ********************** Template *********************** {% if blogs %} <p>{% trans "These are blog posts from everyone:" %}</p> <table> <tr> <td> {% autopaginate blogs_one %} {% for blog_post in blogs %} {% show_blog_post blog_post %} {% endfor %} {% paginate %} </td> <td> {% autopaginate blogs_two %} {% for blog_post in blogs %} {% show_blog_post blog_post %} {% endfor %} {% paginate %} </td> </tr> </table> {% else %} {% trans "No blog posts yet." %} {% endif %} 2009/12/8 Daniel Roseman <dan...@roseman.org.uk>: > On Dec 8, 2:47 am, GoSantoni <mj.schuur...@gmail.com> wrote: >> Guys, i'm struggling with this problem for more than a week now. My >> goal is to use a queryset filter with a foreign key>> Issues are >> 1) column generates a list, what is the right way to get the blog id >> for a post? So whether the post belongs to blog 1 question or blog 2 >> answer? Tried Post.objects.get which fails... >> 2) howto define this in the template >> >> Any help/ directions are appreciated! >> >> ********** First I created this model with a foreign key >> ************************** >> class blog(models.Model): >> function_CHOICES = ( >> (1, _('question')), >> (2, _('answer')), >> ) >> function = models.IntegerField(_('function'), >> choices=function_CHOICES, default=2) >> >> def __unicode__(self): >> return self.get_function_display() >> >> class Post(models.Model): >> blog = models.ForeignKey(blog) >> >> ********* This is the view with the 'column definition' >> ********************** >> def blogs(request, username=None, template_name="blog/blogs.html"): >> blogs = Post.objects.filter(status=2).select_related >> (depth=1).order_by("-publish") >> column = Post.objects.values(blog__id).select_related('blog') >> >> if column == 1: >> blogs = blogs.filter(author=user).exclude(column==2) >> >> pass >> blogs = blogs.filter(author=user).exclude(column==1) >> >> return render_to_response(template_name, { >> "blogs": blogs, >> }, context_instance=RequestContext(request)) >> >> ********* Template. Goal: create 2 columns filtered by whether column >> =1 or 2 ************ >> >> <table> >> <tr> >> <td> >> {% if blogs %} >> <p>{% trans "These are blog posts from everyone:" %}</p> >> {% autopaginate blogs %} >> >> ********** if column == 1 ********** >> {% for blog_post in blogs %} >> {% show_blog_post blog_post %} >> {% endfor %} >> >> {% paginate %} >> {% else %} >> {% trans "No blog posts yet." %} >> {% endif %} >> </td> >> <td> >> {% if blogs %} >> <p>{% trans "These are blog posts from everyone:" %}</p> >> >> ********** condition for passed data >> ********** >> {% autopaginate blogs %} >> {% for blog_post in blogs %} >> {% show_blog_post blog_post %} >> {% endfor %} >> {% paginate %} >> >> {% else %} >> {% trans "No blog posts yet." %} >> {% endif %} >> </td> >> </tr> >> </table> > > It's really unclear what you are trying to do here. You haven't > allowed for any way to pass in a parameter to filter on, so you will > always get all blogs to start with (with posts filtered by user). > 'Column' - ie blog id - is a property of *each post* in the queryset, > and will be different for different posts. > > Is it just that you want to split them up into two columns depending > on the blog id? In which case, you could do it like this: > > blogs_one = Post.objects.filter(author=user, blog__id=1) > blogs_two = Post.objects.filter(author=user, blog__id=2) > > and then iterate through blogs_one and blogs_two in your template. > > There will definitely be better ways of doing this, but without > understanding what you're trying to achieve I can't help further. > -- > DR. > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- Mark Schuuring M: mj.schuur...@gmail.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.