Avoiding code repetition in views

43 views
Skip to first unread message

grimmus

unread,
Jun 28, 2012, 6:20:01 PM6/28/12
to django...@googlegroups.com
Hi,

I am doing a site for a DJ that lists upcoming gigs on each page. There's an include file in each page template to output the list of gigs.

I am wondering how i can pass the gig information to every page without having to repeat the code in every page view.

I hope i have been clear

Thanks

Aaron C. de Bruyn

unread,
Jun 28, 2012, 6:45:55 PM6/28/12
to django...@googlegroups.com
On Thu, Jun 28, 2012 at 3:20 PM, grimmus <graham...@gmail.com> wrote:
> I am wondering how i can pass the gig information to every page without
> having to repeat the code in every page view.
>
> I hope i have been clear

Not really. We can't guess what you're doing. :)
Paste a sample of the code you're referring to so someone can give you pointers.

Christophe Pettus

unread,
Jun 28, 2012, 7:07:44 PM6/28/12
to django...@googlegroups.com
I'm going to take a guess and surmise that you have a lot of different view functions, each one of which needs to pass a list of gigs into the context for the template and then render that page, and you're trying to avoid repeating the code that builds the list of gigs. Is that correct?

You might look at whether you really need all those separate view functions, or if they can be rolled together. You might also look at using class-based Views in 1.4 as a way to factor out the gig-list-building code in a reasonable way.

--
-- Christophe Pettus
x...@thebuild.com

David Lam

unread,
Jun 28, 2012, 10:12:23 PM6/28/12
to django...@googlegroups.com
On Jun 28, 2012, at 3:20 PM, grimmus <graham...@gmail.com> wrote:

> Hi,
>
> I am doing a site for a DJ that lists upcoming gigs on each page. There's an include file in each page template to output the list of gigs.
>
> I am wondering how i can pass the gig information to every page without having to repeat the code in every page view.

Maybe an inclusion tag?

{% get_list_of_cool_gigs page %}

e.g. output the list of gigs for a given argument, the page

J. Cliff Dyer

unread,
Jun 28, 2012, 10:40:30 PM6/28/12
to django...@googlegroups.com
It looks like what you want to do is create a templatetag. Then you can
just include a snippet like: {% gig_schedule %} in your template. The
documentation can point you in the right direction.
> --
> 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/-/naTvLr9T_3MJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users
> +unsub...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.


Ryan Nowakowski

unread,
Jun 28, 2012, 11:45:55 PM6/28/12
to django...@googlegroups.com
Or a custom template context processor that adds gigs to every request
context.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.

kenneth gonsalves

unread,
Jun 29, 2012, 2:01:56 AM6/29/12
to django...@googlegroups.com
On Fri, 2012-06-29 at 01:07 +0200, Christophe Pettus wrote:
> > I hope i have been clear
>
> I'm going to take a guess and surmise that you have a lot of different
> view functions, each one of which needs to pass a list of gigs into
> the context for the template and then render that page, and you're
> trying to avoid repeating the code that builds the list of gigs. Is
> that correct?
>
> You might look at whether you really need all those separate view
> functions, or if they can be rolled together. You might also look at
> using class-based Views in 1.4 as a way to factor out the
> gig-list-building code in a reasonable way.

or use templatetags
--
regards
Kenneth Gonsalves

Timothy Makobu

unread,
Jun 29, 2012, 3:24:44 AM6/29/12
to django...@googlegroups.com
@grimmus

I have a file called shared.py, and In it, i have created a dict template_dict { } which has stuff that all the views share. I import it into views.py and any app that will use that dict. I can then add more items or alter existing ones via template_dict.update().


Melvyn Sopacua

unread,
Jun 29, 2012, 5:48:41 AM6/29/12
to django...@googlegroups.com
On 29-6-2012 5:45, Ryan Nowakowski wrote:
> Or a custom template context processor that adds gigs to every request
> context.

Or just use an iframe in your template that loads the gigs rendering view.
Iframe bad, yes, and all that, but the simplest solution in this case if
your layout can handle it.
A variation is to have them loaded by Ajax, with the drawback that
javascript-disabled browsers miss content. As I browse with noscript all
the time, this isn't a big problem. If I get to a page I'm interested
in, I will grant the domain the privileges in an instant.

Point being, refactoring code duplication isn't limited to the django layer.

--
Melvyn Sopacua


Reply all
Reply to author
Forward
0 new messages