Re: Avoiding code repetition in views
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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
@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(). On Fri, Jun 29, 2012 at 9:01 AM, kenneth gonsalves wrote: > 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 > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@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. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
Or a custom template context processor that adds gigs to every request context. On Thu, Jun 28, 2012 at 10:40:30PM -0400, J. Cliff Dyer wrote: > 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. > > On Thu, 2012-06-28 at 15:20 -0700, grimmus 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. > > > > > > I hope i have been clear > > > > > > Thanks > > > > -- > > 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-users@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. > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@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. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
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. On Thu, 2012-06-28 at 15:20 -0700, grimmus 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. > > > I hope i have been clear > > > Thanks > > -- > 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-users@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. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
On Jun 28, 2012, at 3:20 PM, grimmus 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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
On Jun 29, 2012, at 12:20 AM, grimmus wrote: > 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 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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Avoiding code repetition in views
On Thu, Jun 28, 2012 at 3:20 PM, grimmus 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. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Avoiding code repetition in views
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 -- 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-users@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.
Re: code repetition in views
On Apr 11, 2010, at 5:58 AM, ydjango wrote: I find all my view method have identical code in start and in end: anyway to avoid repetition...? Example: def typical_view_method(request): Check if user is authenticated. Get user and group Get some session variables try: Method specific logic except Exception, e: view_logger.error('error in typical_view_method:%s', e) response = HttpResponse(json_data, mimetype = 'application/json') response.__setitem__('Cache-Control', 'no-store,no-cache') response.__setitem__('Pragma', 'no-cache') response.__setitem__('Expires', '-1') return response I use classes that derive from HttpResponse. Something like: class View(HttpResponse): def content(self): template = loader.get_template(self.template_file) context = RequestContext( self.request, self.context_dict(), processors=self.processors) return template.render(context) # ... more of such class MyView(View): template_file = 'my_template.html' processors= (my_processor,) def __init__(self, request, *args, **kwargs): self.request, self.args, self.kwargs = request, args, kwargs super(MyView, self).__init__(self, self.content()) def context_dict(self): return dict( var1 = 'var1', var2 = 'var2', ) # ... and so on I will probably post a more extensive code snippet to django-dev and django-snippets in a few weeks. Kind regards, Roald -- 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.
Re: code repetition in views
Le 16 avr. 2010 à 20:32, Carl Zmola a écrit : 2) Rewriting your views as classes is the modular way and probably the most flexible. A nice trick with the use of inheritance is to make the class a callable def __call__(self, request, **kwargs): This method provides the view Then you can instantiate the class and call it as if it were a method. MyView=MyViewClass() I didn't even know I could do it ! I have intensively use the @classmethod decorator. and MyView can be used in your urls.py as if it were a normal view. 3) If you want to perform an action before every page is served, you can define middleware to do some processing. For instance you can make a whole site password protected very easily and not need a @login_required decorator. You just have to let the login and logout screens pass through the middleware. This works if you really want to do the same thing for EVERY page. Carl On Apr 10, 11:38 pm, Thierry Chich wrote: I have written my functions as méthods of classes. Then it allow to use inheritance. Le 11 avr. 2010 à 05:58, ydjango a écrit : I find all my view method have identical code in start and in end: anyway to avoid repetition...? Example: def typical_view_method(request): Check if user is authenticated. Get user and group Get some session variables try: Method specific logic except Exception, e: view_logger.error('error in typical_view_method:%s', e) response = HttpResponse(json_data, mimetype = 'application/ json') response.__setitem__('Cache-Control', 'no-store,no-cache') response.__setitem__('Pragma', 'no-cache') response.__setitem__('Expires', '-1') return response -- 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 athttp://groups.google.com/ group/django-users?hl=en . -- Carl Zmola 301-562-1900 x315 czm...@woti.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 . -- 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.
Re: code repetition in views
On 04/12/2010 12:03 PM, zweb wrote: To Summarize, two options are, 1) Template method pattern which Thierry suggested http://en.wikipedia.org/wiki/Template_method_pattern 2) Django Decorators (there is a third way with middleware) 1) Writing a custom decorator is the easiest way to get refactoring benefits. There are some tricks with decorators (using a nested class to bind variables) but these are well documented in normal python documentation. 2) Rewriting your views as classes is the modular way and probably the most flexible. A nice trick with the use of inheritance is to make the class a callable def __call__(self, request, **kwargs): This method provides the view Then you can instantiate the class and call it as if it were a method. MyView=MyViewClass() and MyView can be used in your urls.py as if it were a normal view. 3) If you want to perform an action before every page is served, you can define middleware to do some processing. For instance you can make a whole site password protected very easily and not need a @login_required decorator. You just have to let the login and logout screens pass through the middleware. This works if you really want to do the same thing for EVERY page. Carl On Apr 10, 11:38 pm, Thierry Chich wrote: I have written my functions as méthods of classes. Then it allow to use inheritance. Le 11 avr. 2010 à 05:58, ydjango a écrit : I find all my view method have identical code in start and in end: anyway to avoid repetition...? Example: def typical_view_method(request): Check if user is authenticated. Get user and group Get some session variables try: Method specific logic except Exception, e: view_logger.error('error in typical_view_method:%s', e) response = HttpResponse(json_data, mimetype = 'application/json') response.__setitem__('Cache-Control', 'no-store,no-cache') response.__setitem__('Pragma', 'no-cache') response.__setitem__('Expires', '-1') return response -- 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 athttp://groups.google.com/group/django-users?hl=en . -- Carl Zmola 301-562-1900 x315 czm...@woti.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.
Re: code repetition in views
To Summarize, two options are, 1) Template method pattern which Thierry suggested http://en.wikipedia.org/wiki/Template_method_pattern 2) Django Decorators On Apr 10, 11:38 pm, Thierry Chich wrote: > I have written my functions as méthods of classes. Then it allow to > use inheritance. > > Le 11 avr. 2010 à 05:58, ydjango a écrit : > > > I find all my view method have identical code in start and in end: > > > anyway to avoid repetition...? > > > Example: > > > def typical_view_method(request): > > > Check if user is authenticated. > > Get user and group > > Get some session variables > > > try: > > Method specific logic > > except Exception, e: > > view_logger.error('error in typical_view_method:%s', e) > > > response = HttpResponse(json_data, mimetype = 'application/json') > > response.__setitem__('Cache-Control', 'no-store,no-cache') > > response.__setitem__('Pragma', 'no-cache') > > response.__setitem__('Expires', '-1') > > return response > > > -- > > 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 > > athttp://groups.google.com/group/django-users?hl=en > > . -- 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.
Re: code repetition in views
I have written my functions as méthods of classes. Then it allow to use inheritance. Le 11 avr. 2010 à 05:58, ydjango a écrit : I find all my view method have identical code in start and in end: anyway to avoid repetition...? Example: def typical_view_method(request): Check if user is authenticated. Get user and group Get some session variables try: Method specific logic except Exception, e: view_logger.error('error in typical_view_method:%s', e) response = HttpResponse(json_data, mimetype = 'application/json') response.__setitem__('Cache-Control', 'no-store,no-cache') response.__setitem__('Pragma', 'no-cache') response.__setitem__('Expires', '-1') return response -- 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 . -- 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.
Re: code repetition in views
Django provides a few decorators that remove alot of that repetitive code. Check the auth section for the logged in decorator Vitaly Babiy On Sat, Apr 10, 2010 at 11:58 PM, ydjango wrote: > I find all my view method have identical code in start and in end: > > anyway to avoid repetition...? > > Example: > > def typical_view_method(request): > > Check if user is authenticated. > Get user and group > Get some session variables > > try: > Method specific logic > except Exception, e: > view_logger.error('error in typical_view_method:%s', e) > >response = HttpResponse(json_data, mimetype = 'application/json') >response.__setitem__('Cache-Control', 'no-store,no-cache') >response.__setitem__('Pragma', 'no-cache') >response.__setitem__('Expires', '-1') >return response > > -- > 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. > > -- 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.
code repetition in views
I find all my view method have identical code in start and in end: anyway to avoid repetition...? Example: def typical_view_method(request): Check if user is authenticated. Get user and group Get some session variables try: Method specific logic except Exception, e: view_logger.error('error in typical_view_method:%s', e) response = HttpResponse(json_data, mimetype = 'application/json') response.__setitem__('Cache-Control', 'no-store,no-cache') response.__setitem__('Pragma', 'no-cache') response.__setitem__('Expires', '-1') return response -- 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.