Tuesday, January 22, 2013

Re: What's the right pattern to re-use common view code?

...I can't believe i missed that as a builtin.  Sorry!

Thank you very much!
-andrew

On Tuesday, January 22, 2013 12:01:52 PM UTC-8, Nikolas Stevenson-Molnar wrote:
Django does {% include %} too :) https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#include

You could do something like: {% include "book_list.html" with books=auth.book_set.all %}

_Nik

On 1/22/2013 8:03 AM, andrew jackson wrote:
I have an object that shows up in lots of different parts of the system, say a Book.  

I want to display a list view of Book objects in many different places, e.g., 

When looking at an Author's detail page, I want to see a list of recent books they've written
when looking at a publisher page, similar.
In fact, even when looking at a book i'd like to have a list of books that reference it.

So, there's going to be html code that shows a table of books on several different pages.

My question is, what's the right way to follow DRY w/ django templates and not duplicate the code that makes a list of books?  

If I was using Jinja, it'd be pretty straightforward to {% include %} a snippet in each page that renders each queryset as a fancy table.  It doesn't look like template inheritance is set up that way here, though.

So what's the right way to do it with Django?  Am I thinking about it wrong?  I see a few django-fancy-tables plugins, but they seem pretty heavyweight, and i'd like to understand the right way to approach the solution here.  In fact, I don't even know the right words to use to describe the problem, so my google-fu is weak.  Do I write a custom template tag that takes a queryset as a parameter?  Aren't custom template tags to be avoided?


Thanks much for your time,
Andrew
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/TRxMsFf3sN0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/sSsDumzifngJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment